-1

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

Constantine
  • 1,802
  • 3
  • 23
  • 37

2 Answers2

5

Use the bitwise OR operator:

value |= 0xFFFF

11111111111111112 = 216 - 1= FFFF16

August
  • 12,410
  • 3
  • 35
  • 51
2

OR it with 11111111 11111111. foo | 0xffff

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257