Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:04:37+00:00 2026-06-01T06:04:37+00:00

As the question title reads, assigning 2^31 to a signed and unsigned 32-bit integer

  • 0

As the question title reads, assigning 2^31 to a signed and unsigned 32-bit integer variable gives an unexpected result.

Here is the short program (in C++), which I made to see what’s going on:

#include <cstdio>
using namespace std;

int main()
{
    unsigned long long n = 1<<31;
    long long n2 = 1<<31;  // this works as expected
    printf("%llu\n",n);
    printf("%lld\n",n2);
    printf("size of ULL: %d, size of LL: %d\n", sizeof(unsigned long long), sizeof(long long) );
    return 0;
}

Here’s the output:

MyPC / # c++ test.cpp -o test
MyPC / # ./test
18446744071562067968      <- Should be 2^31 right?
-2147483648               <- This is correct ( -2^31 because of the sign bit)
size of ULL: 8, size of LL: 8

I then added another function p(), to it:

void p()
{
  unsigned long long n = 1<<32;  // since n is 8 bytes, this should be legal for any integer from 32 to 63
  printf("%llu\n",n);
}

On compiling and running, this is what confused me even more:

MyPC / # c++ test.cpp -o test
test.cpp: In function ‘void p()’:
test.cpp:6:28: warning: left shift count >= width of type [enabled by default]
MyPC / # ./test 
0
MyPC /

Why should the compiler complain about left shift count being too large? sizeof(unsigned long long) returns 8, so doesn’t that mean 2^63-1 is the max value for that data type?

It struck me that maybe n*2 and n<<1, don’t always behave in the same manner, so I tried this:

void s()
{
   unsigned long long n = 1;
   for(int a=0;a<63;a++) n = n*2;
   printf("%llu\n",n);
}

This gives the correct value of 2^63 as the output which is 9223372036854775808 (I verified it using python). But what is wrong with doing a left shit?

A left arithmetic shift by n is equivalent to multiplying by 2n
(provided the value does not overflow)

— Wikipedia

The value is not overflowing, only a minus sign will appear since the value is 2^63 (all bits are set).

I’m still unable to figure out what’s going on with left shift, can anyone please explain this?

PS: This program was run on a 32-bit system running linux mint (if that helps)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:04:38+00:00Added an answer on June 1, 2026 at 6:04 am

    On this line:

    unsigned long long n = 1<<32;
    

    The problem is that the literal 1 is of type int – which is probably only 32 bits. Therefore the shift will push it out of bounds.

    Just because you’re storing into a larger datatype doesn’t mean that everything in the expression is done at that larger size.

    So to correct it, you need to either cast it up or make it an unsigned long long literal:

    unsigned long long n = (unsigned long long)1 << 32;
    unsigned long long n = 1ULL << 32;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question title is a bit strange because I'm not exactly sure how to
It is possible that my question title is misleading, but here goes -- I
The question title is rather vague, but here's my situation. I have roughly 700+
OK, the question title is a bit crappy, but I didn't really know how
Question title says it all really - a lot slower than VS2005, with a
Sorry for the long question title. I guess I'm on to a loser on
I know the question title isn't the best. Let me explain. I do a
I guess the question title sums it up. Is there a time when it
I think the question title is clear enough: is is possible to stable_sort() a
I think the question title seems to explain eveything. I want to detect whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.