[Courses] C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, and Variables

Kevin Cole dc.loco at gmail.com
Tue Mar 6 23:49:30 UTC 2012


On Tue, Feb 21, 2012 at 13:40, Sachin Divekar <ssd532 at gmail.com> wrote:

> I tried a very large input to addition program. Following was the result.
>
> ==========================================
> sachin at sachin-ThinkPad-T420:~/projects/C$ ./addition
> Please enter any number up to three digits:
> 1111111111111111111111111
> Please enter another number up to three digits:
> 1111111111111111111111111111111111111111111111
> -1 + -1 = -2
> ===========================================
>
> A quick reading on Wikipedia about C data type "int" says,
>
> "basic signed integer type. At least 16 bits in size."
>
> So, what exactly "16 bits in size" means? I know 1 bit is either state
> 1 or 0. But how this(bits) determines size of number?
>

A bit about bits: If you understand the basic idea of Morse code -- dots
and dashes -- think of them as 0 and 1 instead: A "letter" in Morse code is
comprised of several dots and dashes combined.  Numeric values (and
alphabetic values, and instructions, and memory addresses, and all sorts of
other weird wonderful stuff) in computers is comprised of multiple bits.

Bit: Binary Digit.  Let's say a "dit" is a "Decimal Digit" for comparison:
In our standard Western counting system there are only ten digits: 0, 1, 2,
3, 4, 5, 6, 7, 8, 9.  If I tell you that a calculator can only handle an 8
digit number, how does that determine the size of the number?  In our
day-to-day counting system (base 10, or decimal), each place represents a
power of ten:

So, 247 is 200 + 40 + 7 or (2x10^2) + (4x10^1) + (7x10^0)

In binary (a.k.a. base 2), each position represents a power of 2 -- when
talking about positive integers.  (Negative numbers aren't that hard to
understand, but I'll leave that for another post.)

So, if I say a 4-bit value, or a 10-bit value, or a 16-bit value, I'm
talking about the number of binary digits in the number.  With 4 bits
(unsigned), you can represent positive integers from 0 to 15, like so:

0000 = 0*2^4 + 0*2^3 + 0*2^1 + 0*2^0 = 0 + 0 + 0 + 0 =  0
0001 = 0*2^4 + 0*2^3 + 0*2^1 + 1*2^0 = 0 + 0 + 0 + 1 =  1
0010 = 0*2^4 + 0*2^3 + 1*2^1 + 0*2^0 = 0 + 0 + 2 + 0 =  2
0011 = 0*2^4 + 0*2^3 + 1*2^1 + 1*2^0 = 0 + 0 + 2 + 1 =  3
0100 = 0*2^4 + 1*2^3 + 0*2^1 + 0*2^0 = 0 + 4 + 0 + 0 =  4
0101 = 0*2^4 + 1*2^3 + 0*2^1 + 1*2^0 = 0 + 4 + 0 + 1 =  5
0110 = 0*2^4 + 1*2^3 + 1*2^1 + 0*2^0 = 0 + 4 + 2 + 0 =  6
0111 = 0*2^4 + 1*2^3 + 1*2^1 + 1*2^0 = 0 + 4 + 2 + 1 =  7
1000 = 1*2^4 + 0*2^3 + 0*2^1 + 0*2^0 = 8 + 0 + 0 + 0 =  8
1001 = 1*2^4 + 0*2^3 + 0*2^1 + 0*2^1 = 8 + 0 + 0 + 1 =  9
1010 = 1*2^4 + 0*2^3 + 1*2^1 + 0*2^0 = 8 + 0 + 2 + 0 = 10
1011 = 1*2^4 + 0*2^3 + 1*2^1 + 1*2^0 = 8 + 0 + 2 + 1 = 11
1100 = 1*2^4 + 0*2^3 + 0*2^1 + 0*2^0 = 8 + 4 + 0 + 0 = 12
1101 = 1*2^4 + 1*2^3 + 0*2^1 + 1*2^0 = 8 + 4 + 0 + 1 = 13
1110 = 1*2^4 + 1*2^3 + 1*2^1 + 0*2^0 = 8 + 4 + 2 + 0 = 14
1111 = 1*2^4 + 1*2^3 + 1*2^1 + 1*2^0 = 8 + 4 + 2 + 1 = 15

Notice that the last value above is 2^4 (i.e. 16) minus 1.

With 16 bits, you can represent positive integers between 0 and (2^16 - 1)
which works out to 65,535.

I guess all that was a wee bit much. Wasn't it? ;-)

P.S. In your program, did you tell it that you were inputting binary
numbers?  I mentioned that I didn't want to get into negative integers, but
from the look of your result, you did indeed add the binary value for
negative 1 to the binary value for negative 1, and then printed out the
decimal values of each, plus their sum.  If you want to slog through it on
your own, look up "two's complement" math. (Or I can write  slightly
shorter dissertation on that one. ;-) For now, suffice it to say that
negative numbers in binary are represented in a rather different way and
when you see a slew of ones 11111111111 the mind leaps to "Ah, -1".)
-- 
Physical
[image: [Photo: Kevin Cole]]
Kevin Cole, RHCE <http://launchpad.net/%7Ekjcole/>
Team Contact
Ubuntu Linux DC "LoCo" <http://dc.ubuntu-us.org/>
Washington, DC (US)
Virtual
E-mail:kjcole (at) ubuntu•comWWW:http://dc.ubuntu-us.org/GPG Key ID:
0xE6F332C7 <http://research.gallaudet.edu/%7Ekjcole/pubkey.php>


More information about the Courses mailing list