Java Array Swap swap(byte[] data, int p, int q)

Here you can find the source of swap(byte[] data, int p, int q)

Description

swap

License

Open Source License

Declaration

private static void swap(byte[] data, int p, int q) 

Method Source Code

//package com.java2s;
/**//from  w  ww. j  a  v  a 2 s. c  o m
 * Project: com.dianping.lion.lion-console-0.0.1
 * 
 * File Created at 2012-7-15
 * $Id$
 * 
 * Copyright 2010 dianping.com.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Dianping Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dianping.com.
 */

public class Main {
    private static void swap(byte[] data, int p, int q) {
        int len = data.length * 8;

        for (int i = 0; i < len; i += p) {
            int j = i + q;

            if (j < len) {
                byte b1 = data[i / 8];
                byte b2 = data[j / 8];
                int f1 = b1 & (1 << (i % 8));
                int f2 = b2 & (1 << (j % 8));

                if ((f1 != 0) != (f2 != 0)) {
                    data[i / 8] ^= 1 << (i % 8);
                    data[j / 8] ^= 1 << (j % 8);
                }
            }
        }
    }
}

Related

  1. swap(byte size, byte[] target, short targetOffset, byte[] a, short aOffset)
  2. swap(byte[] b1)
  3. swap(byte[] buffer, int i, int j)
  4. swap(byte[] bytes)
  5. swap(byte[] data)
  6. swap(byte[] rv, int offsetA, int offsetB)
  7. swap(Comparable[] a, int oldIndex, int newIndex)
  8. swap(double x[], Object[] corr, int a, int b)
  9. swap(double[] a, int i, int j)