Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting all bits in int64_t

Tags:

c++

int64

From following code I expect to set all bits in x to 1, but somehow only first 32 bits are set:

int64_t x = 0xFFFFFFFF;
x<<32;
x|=0xFFFFFFFF;

Note: printing x after each line results in 4294967295 (32 lower bits set to 1). Also, tried using numeric_limits<int64_t>::min() with no success. My question is how to set all bits in x? Using RHEL5.5.

Thx

like image 600
R_N Avatar asked Nov 27 '22 17:11

R_N


2 Answers

x<<32 calculates the result of shifting x left by 32 bits and does nothing with the value. You want to use x <<= 32 instead.

like image 116
jwodder Avatar answered Dec 20 '22 01:12

jwodder


Why not int64_t x = -1? or uint64_t x = ~0?

like image 22
paulsm4 Avatar answered Dec 20 '22 00:12

paulsm4