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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:05:55+00:00 2026-05-22T19:05:55+00:00

For a clever and complicated reason that I don’t really want to explain (because

  • 0

For a clever and complicated reason that I don’t really want to explain (because it involves making a timer in an extremely ugly and hacky way), I wrote some C# code sort of like this:

int i = 0;
while (i >= 0) i++; //Should increment forever
Console.Write(i);

I expected the program to hang forever or crash or something, but, to my surprise, after waiting for about 20 seconds or so, I get this ouput:

-2147483648

Well, programming has taught me many things, but I still cannot grasp why continually incrementing a number causes it to eventually be negative…what’s going on here?

  • 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-22T19:05:56+00:00Added an answer on May 22, 2026 at 7:05 pm

    In C#, the built-in integers are represented by a sequence of bit values of a predefined length. For the basic int datatype that length is 32 bits. Since 32 bits can only represent 4,294,967,296 different possible values (since that is 2^32), clearly your code will not loop forever with continually increasing values.

    Since int can hold both positive and negative numbers, the sign of the number must be encoded somehow. This is done with first bit. If the first bit is 1, then the number is negative.

    Here are the int values laid out on a number-line in hexadecimal and decimal:

     Hexadecimal        Decimal
     -----------    -----------
     0x80000000     -2147483648
     0x80000001     -2147483647
     0x80000002     -2147483646
        ...              ...
     0xFFFFFFFE              -2
     0xFFFFFFFF              -1
     0x00000000               0
     0x00000001               1
     0x00000002               2
         ...             ...
     0x7FFFFFFE      2147483646
     0x7FFFFFFF      2147483647
    

    As you can see from this chart, the bits that represent the smallest possible value are what you would get by adding one to the largest possible value, while ignoring the interpretation of the sign bit. When a signed number is added in this way, it is called “integer overflow”. Whether or not an integer overflow is allowed or treated as an error is configurable with the checked and unchecked statements in C#. The default is unchecked, which is why no error occured, but you got that crazy small number in your program.

    This representation is called 2’s Complement.

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

Sidebar

Related Questions

I'm trying to think of clever, clear, and simple ways to write code that
I thought that I'll be clever and use subquery to get my report in
The clever folks at Caucho are quick to point out that Quercus is 3x-5x
Is there any clever way to mix two bit sequences in such way that
Is there some clever content-type setting that makes Firefox display highlighted HTML source code
By clever, I mean NOT TO upload once again the files that has been
Does anybody have any clever solutions that they've used to turn on customErrors for
I have some unicode text that I want to clean up using regular expressions.
Basically I'm trying to write a clever single line of commands that outputs base64
I'm looking for some combination of a plugin or clever programming technique that will

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.