Bitwise (Bitshift) operations with 64-bit integers in C ++

I am trying to deal with bits, which requires me to set a specific bit in an unsigned 64-bit integer. To set bit i , I run a bitwise OR with the bitmap in question with a left shifted number.

#include <stdint.h>
uint64_t kings = 0ULL; // Also tried unsigned long long int before.
kings |= 1 << i;

It works fine from bits 0 to bits 31, but does not work for bits 32 through 63. I suspect because the evaluation of the right side is in a 32-bit integer. So I tried a temporary variable.

uint64_t temp = 0ULL;
temp |= 1 << i;

Perhaps he still evaluates the right side as a 32-bit integer, or that this is another problem that I cannot understand. To print an integer, I use std :: bitset <64>. For instance:

uint64_t kings = 0ULL;
kings |= 1 << 3;
kings |= 1 << 59;

Expected Decimal Value: 576460752303423496

Actual: 8

std::bitset<64> x(kings);
std::cout << x;

: 000000000000000000000000000000000000000000000000000000000000000000000

, | = 1 < 3; .

, 32 63 ?

+4
3

1LL 64- , shift operator <<, 64 :

#include <stdint.h>
uint64_t kings = 0ULL; 
kings |= 1ULL << i;
+7

64- :

kings |= 1i64 << 59;
+3

32 63?

1 int. - LHS ( , ). , 32 , 31 undefined.

64- :

temp |= static_cast<uint64_t>(1) << i;
+3

All Articles