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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:59:30+00:00 2026-05-15T16:59:30+00:00

if i have int temp=(1<<31)>>31. How come the temp becomes -1? how do i

  • 0

if i have int temp=(1<<31)>>31. How come the temp becomes -1?
how do i get around this problem?
thanks

  • 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-15T16:59:30+00:00Added an answer on May 15, 2026 at 4:59 pm

    Ints are signed by default, which usually means that the high bit is reserved to indicate whether the integer is negative or not. Look up Two’s complement for an explanation of how this works.

    Here’s the upshot:

    [steven@sexy:~]% cat test.c
    #include <stdint.h>
    #include <stdio.h>
    
    int main(int argc, char **argv[]) {
        uint32_t uint;
        int32_t sint;
        int64_t slong;
        uint = (((uint32_t)1)<<31) >> 31;
        sint = (1<<31) >> 31;
        slong = (1L << 31) >> 31;
        printf("signed 32 = %d, unsigned 32 = %u, signed 64 = %ld\n", sint, uint, slong);
    }
    
    [steven@sexy:~]% ./test
    signed 32 = -1, unsigned 32 = 1, signed 64 = 1
    

    Notice how you can avoid this problem either by using an “unsigned” int (allowing the use of all 32 bits), or by going to a larger type which you don’t overflow.

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

Sidebar

Related Questions

In C++, the following lines have me confused: int temp = (int)(0×00); int temp
I've defined some signal: typedef boost::signals2::signal<void (int temp)> SomeSig; typedef SomeSig::slot_type SomeSigType; I have
I have a program that sorts input lines lexicographically, and I've come to this
I have come across a strange problem. MY app has been localized to support
I have this code main.cpp #include <iostream> #include functs.h using namespace std; int main()
I have a function in C++ such as: void* getField(interface* p) { int* temp
i have some variables in an app like : public int temp = 10
i have recently come to this error , i search a lot but i
Let's say I have int a() { /* Tons of code ....*/ return someInt;
I have an int array as a property of a Web User Control. I'd

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.