package ro.pub.cs.pp.a51hadoop.algorithm.a51; public class A51Bitops { public static int reverse_bits_int(int x) { int nr_bits = 32; int ret = 0; for (int i = 0; i < nr_bits; i++) if ((x & 1 << i) != 0) ret |= 1 << (nr_bits - 1 - i); return ret; } public static long reverse_bits_long(long x) { int nr_bits = 64; long ret = 0; long one = 1; for (int i = 0; i < nr_bits; i++) if ((x & one << i) != 0) ret |= one << (nr_bits - 1 - i); return ret; } public static int parity(int x) { x ^= x >> 16; x ^= x >> 8; x ^= x >> 4; x ^= x >> 2; x ^= x >> 1; return x & 1; } /* Clock one shift register */ public static int clockone(int reg, int mask, int taps) { int t = reg & taps; reg = (reg << 1) & mask; reg |= parity(t); return reg; } /* * @return 1 or 0 as the i-th bit of x is 1 or 0 */ public static int get_ith_bit(int x, int i) { return (x & (1 << i)) >> i; } }