Bit Patterns and Overflow
A signed integer is 32 bits wide and can store values in the range: -2,147,483,648 to 2,147,483,627A signed char is 8 bits wide and can store values in the range: -128 to 127
What happens when you try to store a value that is too large for the data type? With unsigned values, it just "wraps" back around to 0. Think of the bits being sort of like an odometer on a car. Once the odometer gets to 999999, it will "wrap" back around to 0. So, an unsigned char with a value of 255 will become 0:
11111111 + 1 --------- 000000000
With signed numbers, the result is undefined. It could be anything and do anything, including crashing the program. It's up to the particular compiler. The GNU gcc compiler does a sort of "wrapping" itself. The difference is that instead of going from the largest positive value back to 0, the bits go from the largest positive value to the smallest negative value. (e.g. 127 + 1 is -128).
This is a sample run showing what happens when you keep incrementing a signed char: (Keep in mind that compilers can do different things when a signed integral value overflows.)
int char bits int char bits int char bits int char bits int char bits ------------------ ------------------ ------------------ ------------------ ------------------ 0 0 00000000 100 100 01100100 200 -56 11001000 300 44 00101100 400 -112 10010000 1 1 00000001 101 101 01100101 201 -55 11001001 301 45 00101101 401 -111 10010001 2 2 00000010 102 102 01100110 202 -54 11001010 302 46 00101110 402 -110 10010010 3 3 00000011 103 103 01100111 203 -53 11001011 303 47 00101111 403 -109 10010011 4 4 00000100 104 104 01101000 204 -52 11001100 304 48 00110000 404 -108 10010100 5 5 00000101 105 105 01101001 205 -51 11001101 305 49 00110001 405 -107 10010101 6 6 00000110 106 106 01101010 206 -50 11001110 306 50 00110010 406 -106 10010110 7 7 00000111 107 107 01101011 207 -49 11001111 307 51 00110011 407 -105 10010111 8 8 00001000 108 108 01101100 208 -48 11010000 308 52 00110100 408 -104 10011000 9 9 00001001 109 109 01101101 209 -47 11010001 309 53 00110101 409 -103 10011001 10 10 00001010 110 110 01101110 210 -46 11010010 310 54 00110110 410 -102 10011010 11 11 00001011 111 111 01101111 211 -45 11010011 311 55 00110111 411 -101 10011011 12 12 00001100 112 112 01110000 212 -44 11010100 312 56 00111000 412 -100 10011100 13 13 00001101 113 113 01110001 213 -43 11010101 313 57 00111001 413 -99 10011101 14 14 00001110 114 114 01110010 214 -42 11010110 314 58 00111010 414 -98 10011110 15 15 00001111 115 115 01110011 215 -41 11010111 315 59 00111011 415 -97 10011111 16 16 00010000 116 116 01110100 216 -40 11011000 316 60 00111100 416 -96 10100000 17 17 00010001 117 117 01110101 217 -39 11011001 317 61 00111101 417 -95 10100001 18 18 00010010 118 118 01110110 218 -38 11011010 318 62 00111110 418 -94 10100010 19 19 00010011 119 119 01110111 219 -37 11011011 319 63 00111111 419 -93 10100011 20 20 00010100 120 120 01111000 220 -36 11011100 320 64 01000000 420 -92 10100100 21 21 00010101 121 121 01111001 221 -35 11011101 321 65 01000001 421 -91 10100101 22 22 00010110 122 122 01111010 222 -34 11011110 322 66 01000010 422 -90 10100110 23 23 00010111 123 123 01111011 223 -33 11011111 323 67 01000011 423 -89 10100111 24 24 00011000 124 124 01111100 224 -32 11100000 324 68 01000100 424 -88 10101000 25 25 00011001 125 125 01111101 225 -31 11100001 325 69 01000101 425 -87 10101001 26 26 00011010 126 126 01111110 226 -30 11100010 326 70 01000110 426 -86 10101010 27 27 00011011 127 127 01111111 227 -29 11100011 327 71 01000111 427 -85 10101011 28 28 00011100 128 -128 10000000 228 -28 11100100 328 72 01001000 428 -84 10101100 29 29 00011101 129 -127 10000001 229 -27 11100101 329 73 01001001 429 -83 10101101 30 30 00011110 130 -126 10000010 230 -26 11100110 330 74 01001010 430 -82 10101110 31 31 00011111 131 -125 10000011 231 -25 11100111 331 75 01001011 431 -81 10101111 32 32 00100000 132 -124 10000100 232 -24 11101000 332 76 01001100 432 -80 10110000 33 33 00100001 133 -123 10000101 233 -23 11101001 333 77 01001101 433 -79 10110001 34 34 00100010 134 -122 10000110 234 -22 11101010 334 78 01001110 434 -78 10110010 35 35 00100011 135 -121 10000111 235 -21 11101011 335 79 01001111 435 -77 10110011 36 36 00100100 136 -120 10001000 236 -20 11101100 336 80 01010000 436 -76 10110100 37 37 00100101 137 -119 10001001 237 -19 11101101 337 81 01010001 437 -75 10110101 38 38 00100110 138 -118 10001010 238 -18 11101110 338 82 01010010 438 -74 10110110 39 39 00100111 139 -117 10001011 239 -17 11101111 339 83 01010011 439 -73 10110111 40 40 00101000 140 -116 10001100 240 -16 11110000 340 84 01010100 440 -72 10111000 41 41 00101001 141 -115 10001101 241 -15 11110001 341 85 01010101 441 -71 10111001 42 42 00101010 142 -114 10001110 242 -14 11110010 342 86 01010110 442 -70 10111010 43 43 00101011 143 -113 10001111 243 -13 11110011 343 87 01010111 443 -69 10111011 44 44 00101100 144 -112 10010000 244 -12 11110100 344 88 01011000 444 -68 10111100 45 45 00101101 145 -111 10010001 245 -11 11110101 345 89 01011001 445 -67 10111101 46 46 00101110 146 -110 10010010 246 -10 11110110 346 90 01011010 446 -66 10111110 47 47 00101111 147 -109 10010011 247 -9 11110111 347 91 01011011 447 -65 10111111 48 48 00110000 148 -108 10010100 248 -8 11111000 348 92 01011100 448 -64 11000000 49 49 00110001 149 -107 10010101 249 -7 11111001 349 93 01011101 449 -63 11000001 50 50 00110010 150 -106 10010110 250 -6 11111010 350 94 01011110 450 -62 11000010 51 51 00110011 151 -105 10010111 251 -5 11111011 351 95 01011111 451 -61 11000011 52 52 00110100 152 -104 10011000 252 -4 11111100 352 96 01100000 452 -60 11000100 53 53 00110101 153 -103 10011001 253 -3 11111101 353 97 01100001 453 -59 11000101 54 54 00110110 154 -102 10011010 254 -2 11111110 354 98 01100010 454 -58 11000110 55 55 00110111 155 -101 10011011 255 -1 11111111 355 99 01100011 455 -57 11000111 56 56 00111000 156 -100 10011100 256 0 00000000 356 100 01100100 456 -56 11001000 57 57 00111001 157 -99 10011101 257 1 00000001 357 101 01100101 457 -55 11001001 58 58 00111010 158 -98 10011110 258 2 00000010 358 102 01100110 458 -54 11001010 59 59 00111011 159 -97 10011111 259 3 00000011 359 103 01100111 459 -53 11001011 60 60 00111100 160 -96 10100000 260 4 00000100 360 104 01101000 460 -52 11001100 61 61 00111101 161 -95 10100001 261 5 00000101 361 105 01101001 461 -51 11001101 62 62 00111110 162 -94 10100010 262 6 00000110 362 106 01101010 462 -50 11001110 63 63 00111111 163 -93 10100011 263 7 00000111 363 107 01101011 463 -49 11001111 64 64 01000000 164 -92 10100100 264 8 00001000 364 108 01101100 464 -48 11010000 65 65 01000001 165 -91 10100101 265 9 00001001 365 109 01101101 465 -47 11010001 66 66 01000010 166 -90 10100110 266 10 00001010 366 110 01101110 466 -46 11010010 67 67 01000011 167 -89 10100111 267 11 00001011 367 111 01101111 467 -45 11010011 68 68 01000100 168 -88 10101000 268 12 00001100 368 112 01110000 468 -44 11010100 69 69 01000101 169 -87 10101001 269 13 00001101 369 113 01110001 469 -43 11010101 70 70 01000110 170 -86 10101010 270 14 00001110 370 114 01110010 470 -42 11010110 71 71 01000111 171 -85 10101011 271 15 00001111 371 115 01110011 471 -41 11010111 72 72 01001000 172 -84 10101100 272 16 00010000 372 116 01110100 472 -40 11011000 73 73 01001001 173 -83 10101101 273 17 00010001 373 117 01110101 473 -39 11011001 74 74 01001010 174 -82 10101110 274 18 00010010 374 118 01110110 474 -38 11011010 75 75 01001011 175 -81 10101111 275 19 00010011 375 119 01110111 475 -37 11011011 76 76 01001100 176 -80 10110000 276 20 00010100 376 120 01111000 476 -36 11011100 77 77 01001101 177 -79 10110001 277 21 00010101 377 121 01111001 477 -35 11011101 78 78 01001110 178 -78 10110010 278 22 00010110 378 122 01111010 478 -34 11011110 79 79 01001111 179 -77 10110011 279 23 00010111 379 123 01111011 479 -33 11011111 80 80 01010000 180 -76 10110100 280 24 00011000 380 124 01111100 480 -32 11100000 81 81 01010001 181 -75 10110101 281 25 00011001 381 125 01111101 481 -31 11100001 82 82 01010010 182 -74 10110110 282 26 00011010 382 126 01111110 482 -30 11100010 83 83 01010011 183 -73 10110111 283 27 00011011 383 127 01111111 483 -29 11100011 84 84 01010100 184 -72 10111000 284 28 00011100 384 -128 10000000 484 -28 11100100 85 85 01010101 185 -71 10111001 285 29 00011101 385 -127 10000001 485 -27 11100101 86 86 01010110 186 -70 10111010 286 30 00011110 386 -126 10000010 486 -26 11100110 87 87 01010111 187 -69 10111011 287 31 00011111 387 -125 10000011 487 -25 11100111 88 88 01011000 188 -68 10111100 288 32 00100000 388 -124 10000100 488 -24 11101000 89 89 01011001 189 -67 10111101 289 33 00100001 389 -123 10000101 489 -23 11101001 90 90 01011010 190 -66 10111110 290 34 00100010 390 -122 10000110 490 -22 11101010 91 91 01011011 191 -65 10111111 291 35 00100011 391 -121 10000111 491 -21 11101011 92 92 01011100 192 -64 11000000 292 36 00100100 392 -120 10001000 492 -20 11101100 93 93 01011101 193 -63 11000001 293 37 00100101 393 -119 10001001 493 -19 11101101 94 94 01011110 194 -62 11000010 294 38 00100110 394 -118 10001010 494 -18 11101110 95 95 01011111 195 -61 11000011 295 39 00100111 395 -117 10001011 495 -17 11101111 96 96 01100000 196 -60 11000100 296 40 00101000 396 -116 10001100 496 -16 11110000 97 97 01100001 197 -59 11000101 297 41 00101001 397 -115 10001101 497 -15 11110001 98 98 01100010 198 -58 11000110 298 42 00101010 398 -114 10001110 498 -14 11110010 99 99 01100011 199 -57 11000111 299 43 00101011 399 -113 10001111 499 -13 11110011
For a fun and interesting challenge: Write a program to generate the table above. That's what I did. I was too lazy to type all of that in manually!