Last change
on this file since 166 was
166,
checked in by (none), 14 years ago
|
HadoopA51: imported git repository from github
|
File size:
947 bytes
|
Rev | Line | |
---|
[166] | 1 | package ro.pub.cs.pp.a51hadoop.algorithm.a51; |
---|
| 2 | |
---|
| 3 | public class A51Bitops |
---|
| 4 | { |
---|
| 5 | |
---|
| 6 | public static int reverse_bits_int(int x) |
---|
| 7 | { |
---|
| 8 | int nr_bits = 32; |
---|
| 9 | int ret = 0; |
---|
| 10 | for (int i = 0; i < nr_bits; i++) |
---|
| 11 | if ((x & 1 << i) != 0) |
---|
| 12 | ret |= 1 << (nr_bits - 1 - i); |
---|
| 13 | return ret; |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | public static long reverse_bits_long(long x) |
---|
| 17 | { |
---|
| 18 | int nr_bits = 64; |
---|
| 19 | long ret = 0; |
---|
| 20 | long one = 1; |
---|
| 21 | for (int i = 0; i < nr_bits; i++) |
---|
| 22 | if ((x & one << i) != 0) |
---|
| 23 | ret |= one << (nr_bits - 1 - i); |
---|
| 24 | return ret; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | public static int parity(int x) |
---|
| 28 | { |
---|
| 29 | x ^= x >> 16; |
---|
| 30 | x ^= x >> 8; |
---|
| 31 | x ^= x >> 4; |
---|
| 32 | x ^= x >> 2; |
---|
| 33 | x ^= x >> 1; |
---|
| 34 | return x & 1; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | /* Clock one shift register */ |
---|
| 39 | public static int clockone(int reg, int mask, int taps) |
---|
| 40 | { |
---|
| 41 | int t = reg & taps; |
---|
| 42 | reg = (reg << 1) & mask; |
---|
| 43 | reg |= parity(t); |
---|
| 44 | return reg; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | /* |
---|
| 50 | * @return 1 or 0 as the i-th bit of x is 1 or 0 |
---|
| 51 | */ |
---|
| 52 | public static int get_ith_bit(int x, int i) |
---|
| 53 | { |
---|
| 54 | return (x & (1 << i)) >> i; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | } |
---|
| 58 | |
---|
Note: See
TracBrowser
for help on using the repository browser.