Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Number Removing First Bit - Fast Way JAVA

Tags:

java

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" ?

like image 300
Arpssss Avatar asked Feb 27 '26 23:02

Arpssss


1 Answers

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;
like image 112
twain249 Avatar answered Mar 01 '26 12:03

twain249



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!