View Single Post
 03-27-2013, 09:16 PM PM User | #5 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts It's probably easier to see this if you look at a TINYINT, a one-byte value. One byte is 8 bits. One of those bits has to be used to hold the SIGN of the number. Traditionally (and in all modern hardware) it is bit 0. So some of the possible bit patterns: 0000 0000 == 0 decimal 0000 0001 == 1 decimal 0111 1111 == 127 decimal 1000 0000 == -128 decimal 1000 0001 == -127 decimal 1111 1111 == -1 decimal Now, look what happens if (ignoring the sign) you increment 127 decimal (0111 1111) by 1: The value becomes 1000 0000 which is -128 decimal! So incrementing the maximum positive number by one produces the maximum negative number!! As Felgall and DJM said, if you instead use an UNSIGNED number -- here we will use UNSIGNED TINYINT for illustration -- you double (plus 1) the range of positive values. 0000 0000 == 0 decimal 0000 0001 == 1 decimal 0111 1111 == 127 decimal 1000 0000 == 128 decimal 1000 0001 == 129 decimal 1111 1111 == 255 decimal Same exact thing happens with BIGINT except the decimal numbers involved are much bigger. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 Users who have thanked Old Pedant for this post: cancer10 (03-28-2013)