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

  • Home
  • SEARCH
  • 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 4548538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:14:05+00:00 2026-05-21T16:14:05+00:00

Though the two snippets below have a slight difference in the manipulation of the

  • 0

Though the two snippets below have a slight difference in the manipulation of the find variable, still the output seems to be the same. Why so?

First Snippet

    #include<iostream>
    using namespace std;
    int main()
    {

    int number = 3,find;
    find = number << 31;
    find *= -1;
    cout << find;
    return 0;
    } 

Second Snippet

   #include<iostream>
   using namespace std;
   int main()
   {
   int number = 3,find;
   find = number << 31;
   find *= 1;
   cout << find;
   return 0;
   } 

Output for both snippets:

-2147483648

(according to Ideone: 1, 2)

  • 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-21T16:14:06+00:00Added an answer on May 21, 2026 at 4:14 pm

    In both your samples, assuming 32bit ints, you’re invoking undefined behavior as pointed out in Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?

    Why? Because number is a signed int, with 32bits of storage. (3<<31) is not representable in that type.

    Once you’re in undefined behavior territory, the compiler can do as it pleases.

    (You can’t rely on any of the following because it is UB – this is just an observation of what your compiler appears to be doing).

    In this case it looks like the compiler is doing the right shift, resulting in 0x80000000 as a binary representation. This happens to be the two’s complement representation of INT_MIN. So the second snippet is not surprising.

    Why does the first one output the same thing? In two’s complement, MIN_INT would be -2^31. But the max value is 2^31-1. MIN_INT * -1 would be 2^31 (if it was representable). And guess what representation that would have? 0x80000000. Back to where you started!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two radio buttons next to each other, and even though each one
This is a two part question. The first part is converting the function below
Please consider the two snippets of code (the first prints Local eval, the second
I have these two snippets of code (written in Java) which is more more
When I run the two files below through the command line, (first start socket_server,
I have two models and controllers: Snippets, and Tags. There is a belongs_to relationship,
I have two models -- User and Entry -- that are related through a
I have two spring config files and I'm specifying them in my web.xml as
Consider the two following regular expression snippets and dummy HTML that it should be
I have to develop a custom collection of objects. The reason is two fold,

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.