Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
bit shifting with unsigned long type produces wrong results

I'm a bit confused because I wanted to initialize a variable of type unsigned long whose size is 8 bytes on my system (on every modern system I suppose). When I want to assign 1 << 63 to the variable, I get a compiler warning however and the number is in fact 0. When I do 1 << 30 I get the expected result of 2 ^ 30 = 1073741824. Yet when I do 1 << 31, I get the result of 2 ^ 64 (I think; actually this shouldn't be possible) which prints 18446744071562067968.

Can anyone explain this behaviour to me?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

1 << 63 will be computed in int arithmetic, and your int is probably 32 bit.

Remedy this by promoting one of the arguments: 1ULL << 63 will do it.

ULL means the expression will be at least 64 bits.

over 4 years ago · Santiago Trujillo Report

0

The expression 1 << 63 has type int. The range of an int is -231 … 231 - 1 on most systems, 263 is too large for that. Try either (unsigned long)1 << 63 or 1UL << 63 to shift a value of type unsigned long left by 63 places.

over 4 years ago · Santiago Trujillo Report

0

The 1 here, is called an integer constant. By the norms specified in the standard, C11, chapter §6.4.4.1 the syntax for the same is

integer-constant:
     decimal-constant integer-suffixopt
     octal-constant integer-suffixopt
      hexadecimal-constant integer-suffixopt

and regarding the Semantics,

The type of an integer constant is the first of the corresponding list in which its value can be represented.

and the table says, if there is no suffix, and the value is representable in the int range it should be considered as int. So, 1 here, is considered an int, which is of generally 4 bytes, or 32 bits, also same in your case.

To explicitly specify the 1 as unsigned long (64) bit, we can use the suffix, like

1UL << 63

should solve your issue.

Please note: unsigned long is not guaranteed to be of 64 bits. unsigned long long is guaranteed to have at least 64 bits. However, as long as you're using the platform where unsigned long is of 64 bits , you should be fine

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!