Integer: Difference between revisions

m
No edit summary
m (→‎Two's Complement: formatting)
Line 12:
 
==Two's Complement==
This is the method computers use to represent negative numbers. The leftmost bit of a number's binary representation is often called the "sign bit" and determines whether the number is positive or negative. For 32-bit integers, <tt>0x7FFFFFFF</tt> (2,147,483,647 in decimal) represents the largest positive signed integer, and <tt>0x80000000</tt> (-2,147,483,648 in decimal) represents the smallest negative signed integer. (If you've seen these numbers in programming bugs before, there's a reason for that - which will be explained later.) With two's complement, you can easily change a positive number to a negative number by flipping the bits (turning every 0 to a 1 and vice versa), then adding 1 to the result. Many CPUs have dedicated instructions just to do this. This system allows integer math to work the way you would expect it to in real life. There's just one small problem...
 
==Integer Overflow==
1,489

edits