Set the rest of the bits to 1 in Java

81 views Asked by At

I need some help with bitwise operations. I have number(64 bit) were first 16 bits are meaningful, and I'd like to set rest of them to "1"

00000000 11000001 00000000 00000000 ... <- currrent value

00000000 11000001 11111111 11111111 ... <- result I am trying to achieve

P.S. Oh, yeah! Sometimes, to solve - you just need to write out your task:)

I got it: value |= (-1 << bitsCount);

bitsCount - count of my meaningful bits

2

There are 2 answers

0
August On BEST ANSWER

Use the bitwise OR operator:

value |= 0xFFFF

11111111111111112 = 216 - 1= FFFF16

0
Steve Kuo On

OR it with 11111111 11111111. foo | 0xffff