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 6907015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:22:59+00:00 2026-05-27T08:22:59+00:00

I am new to bit manipulations tricks and I wrote a simple code to

  • 0

I am new to bit manipulations tricks and I wrote a simple code to see the output of doing single bit shifts on a single number viz. 2

#include <iostream>
int main(int argc, char *argv[])
{

  int num=2;

 do
   {
     std::cout<<num<<std::endl;
     num=num<<1;//Left shift by 1 bit.

   } while (num!=0);


  return 0;
}

The output of this is the following.

2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
-2147483648

Obviously, continuously bit shifting to the left by 1 bit, will result in zero as it has done above, but why does the computer output a negative number at the very end before terminating the loop (since num turned zero)??

However when I replace int num=2 by unsigned int num=2 then I get the same output except
that the last number is this time displayed as positive i.e. 2147483648 instead of -2147483648

I am using the gcc compiler on Ubuntu Linux

  • 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-05-27T08:22:59+00:00Added an answer on May 27, 2026 at 8:22 am

    That’s because int is a signed integer. In the two’s-complement representation, the sign of the integer is determined by the upper-most bit.

    Once you have shifted the 1 into the highest (sign) bit, it flips negative.

    When you use unsigned, there’s no sign bit.

    0x80000000 = -2147483648 for a signed 32-bit integer.
    0x80000000 =  2147483648 for an unsigned 32-bit integer.
    

    EDIT :

    Note that strictly speaking, signed integer overflow is undefined behavior in C/C++. The behavior of GCC in this aspect is not completely consistent:

    • num = num << 1; or num <<= 1; usually behaves as described above.
    • num += num; or num *= 2; may actually go into an infinite loop on GCC.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a new bit in Qt... I have a Qt GUI application (written by
A bit new with WPF...It seems like not matter what I change with horizontal
Still a bit new to Linq. This is driving me nuts. I want to
I am bit new to C#. I am working on a Database application and
im a bit new at extension development, but im about to embark upon a
I am a bit new to Perl, but here is what I want to
I'm a bit new to WCF and I don't think I completely understand what
I'm a bit new to .Net development, been working in Java for some time
I'm a bit new to Papervision3D, and I'm trying to set the alpha level
I'm a bit new to SQL and have trouble constructing a select statement. I

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.