Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bit shift a long by more than 32 bits? [duplicate]

Tags:

c++

c

It seems like I should be able to perform bit shift in C/C++ by more than 32 bits provided the left operand of the shift is a long. But this doesn't seem to work, at least with the g++ compiler.

Example:

unsigned long A = (1L << 37)

gives

A = 0

which isn't what I want. Am I missing something or is this just not possible?

-J


1 Answers

A is equal to 0 because A only has 32-bits, so of course you are shifting all the bits off to the left leaving only 0 bits left. You need to make A 64-bit:

unsigned long long A = (1ULL << 37);

Or if you intend to use Visual C++:

unsigned __int64 A = (1ULL << 37);
like image 82
Cthutu Avatar answered Sep 09 '25 10:09

Cthutu