I have a series of long numbers in a long array. I want to take each Long number and want to get the first bit of it and according to the first bit, I want to process the other bits. Like following pseudo code,
i = 1000000 ;
long[] a = new long[i];
for j = 0 to i
do,
get long lo = a[i];
// get first bit of lo
if first bit = 0
print long number (by removing first bit) in file a1
else
print long number (by removing first bit) in file a2
Can anybody help me, what is the fastest way to, "get the first bit of that long number" and "removing the first bit and get the number" ?
bit mask the number with 1.
long temp = a[i];
int bit = (temp >> 63) & 1;
this will shift the number over 63 places and bitwise and the number with 1. This will be 1 if the bit is 1 and 0 if the bit is 0.
if you want the lowest bit you don't need to shift
int bit = temp & 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With