Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Great questions. K&amp;R was written back in the days when there was a lot less to know about computers, and so programmers knew a lot more about the hardware. Every programmer <em>ought</em> to be familiar with this stuff, but (understandably) many beginning programmers aren't.</p> <p>At Carnegie Mellon University they developed an entire course to fill in this gap in knowledge, which I was a TA for. I recommend the textbook for that class: "Computer Systems: A Programmer's Perspective" <a href="http://amzn.com/013034074X/" rel="noreferrer">http://amzn.com/013034074X/</a></p> <p>The answers to your questions are longer than can really be covered here, but I'll give you some brief pointers for your own research.</p> <p>Basically, computers store all information--whether in memory (RAM) or on disk--in binary, a base-2 number system (as opposed to decimal, which is base 10). One binary digit is called a bit. Computers tend to work with memory in 8-bit chunks called bytes.</p> <p>A char in C is one byte. An int is typically four bytes (although it can be different on different machines). So a char can hold only 256 possible values, 2^8. An int can hold 2^32 different values.</p> <p>For more, definitely read the book, or read a few Wikipedia pages:</p> <ul> <li><a href="http://en.wikipedia.org/wiki/Binary_numeral_system" rel="noreferrer">http://en.wikipedia.org/wiki/Binary_numeral_system</a></li> <li><a href="http://en.wikipedia.org/wiki/Twos_complement" rel="noreferrer">http://en.wikipedia.org/wiki/Twos_complement</a></li> </ul> <p>Best of luck!</p> <p><strong>UPDATE</strong> with info on modular arithmetic as requested:</p> <p>First, read up on modular arithmetic: <a href="http://en.wikipedia.org/wiki/Modular_arithmetic" rel="noreferrer">http://en.wikipedia.org/wiki/Modular_arithmetic</a></p> <p>Basically, in a two's complement system, an n-bit number really represents an equivalence class of integers modulo 2^n.</p> <p>If that seems to make it more complicated instead of less, then the key things to know are simply:</p> <ul> <li>An unsigned n-bit number holds values from 0 to 2^n-1. The values "wrap around", so e.g., when you add two numbers and get 2^n, you really get zero. (This is called "overflow".)</li> <li>A signed n-bit number holds values from -2^(n-1) to 2^(n-1)-1. Numbers still wrap around, but the highest number wraps around to the most negative, and it starts counting up towards zero from there.</li> </ul> <p>So, an unsigned byte (8-bit number) can be 0 to 255. 255 + 1 wraps around to 0. 255 + 2 ends up as 1, and so forth. A signed byte can be -128 to 127. 127 + 1 ends up as -128. (!) 127 + 2 ends up as -127, etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload