== Simple Binary ==

Base 2 (binary) only has 1's and 0's.

Each bit  (1 or 0) represents a power of 2, e.g.  1 , 2 , 4 , 8 , ...

For example:

     0 0 0 0 1 1 1 1 = 8 + 4 + 2 + 1 = 15 decimal

     0 1 0 1 1 0 0 1  =  64 + 16 + 8 + 1 = 89 decimal

A byte contains 8 bits.  So the largest number that can be stored in a byte is:

     1 1 1 1 1 1 1 1  =  128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 decimal

Numbers like this are unsigned integers - that means always positive.


== Negatives ==

Signed numbers can be either negative or positive.  To make negative integers,
the computer treats the MSB (most significant bit) as a negative value.
In a byte, the MSB has a value of -128 .

     0 0 0 0 1 1 1 1  =  8 + 4 + + 1 = 15 decimal

     1 0 0 0 1 1 1 1  =  -128 + 8 + 4 + + 1 = -113 decimal

So, is the MSB simply a negative sign?  No, it also has a value.

This system is called two's complement.

The highest positive byte in two's complement is 01111111 = 127 dec

The lowest negative byte in two's complement is 10000000 = -128 dec

What is the decimal equivalent of  11111111 bin ?
             -128 + 64 + 32 ... =  -1  dec


== Bigger Integers ==

In Java, an int variable is stored in 32 bits (4 bytes).
Then the Most Significant Bit has a value of   -2^31  in two's complement.

      0111111 11111111 11111111 11111111 bin = 2^31 - 1 = approx 2 billion   
        ... that's the highest positive number that can be stored in 32 bits.

      10000000 00000000 00000000 00000000 bin = -2^31 = approx -2 billion   
        ... that's the lowest negative number that can be stored in 32 bits. 

In Java a long integer can store larger numbers using 64 bits.  
The largest positive 64 bit number that can be stored is 2^63-1 ,
and the lowest negative number is  -2^63


== Converting Numbers ==

Converting binary to decimal is easy - just add up the powers of 2.
Remember that the MSB is negative (unless it is running in unsigned mode).

      0 1 0 1 0 0 1 1 =  64 + 16 + 2  + 1  = 83 dec

      1 0 1 0 1 1 0 0 =  -128  +  32  +  8  +  4  =  -84 dec

Converting decimal to binary is harder.  You must ask yourself:
"What powers of 2 add up to this number?"  That's not always easy.

      Convert  50 dec  to binary.
      -  won't need 128 or 64, so start with 32
      -  50 = 32 + 18
      -  For the 18, we need  16 + 2
      ==>    50 dec =  0 0 1 1 0 0 1 0  bin

== Quick Trick ==

The easier way to change decimal to binary is:

Try converting 91 to binary:

       91 / 2 = 45  rem  1

       45 / 2 = 22  rem  1

       22 / 2 = 11  rem  0

       11 / 2 =  5   rem  1

         5 / 2 =  2   rem  1

         2 / 2 =  1   rem  0

         1 / 2 =  0   rem  1     

        Now read the remainders from the bottom up.

              91 dec =  1 0 1 1 0 1 1  bin  =  64 + 16 + 8 + 2 + 1


== Arithmetic ==

The CPU (microprocessor) contains dedicated circuits for arithmetic.
This must be highly automated, performing over 1000 MIPS (million instructions per second).

- Addition -

Adding binary numbers works like this:
 - start at the right hand end
     (the LSB =
least significant bit)
 - add the bits
    write the second bit of the answer
    
carry the rest if necessary
 - move one place to the left
    add the carry and the two bits
    write second bit
    carry if necessary

  

    MSB                LSB                         Decimal

         0 0 0 1 0 1 0 1                                 21
      + 0 0 1 1 0 1 1 0                              + 
54
      --------------------                           ---------
         
0 1 0 0 1 0 1 1  <== Start here         75
===========================
         1 0 0 0 0 1 1 0                                
-122  
      + 0 0 1 1 1 1 1 0                               +  
62
      --------------------                             ---------
         
1 1 0 0 0 1 0 0  <== Start here         - 60


== Counting in Binary ==

Counting is the same as adding 1.  You can practice by adding in your head,
and clicking on the blue boxes. 

1 - 10 dec

  0 0 0 0 0 0 0 1
  0 0 0 0 0 0 1 0
  0 0 0 0 0 0 1 1
  0 0 0 0 0 1 0 0
  0 0 0 0 0 1 0 1
  0 0 0 0 0 1 1 0
  0 0 0 0 0 1 1 1
  0 0 0 0 1 0 0 0
  0 0 0 0 1 0 0 1
  0 0 0 0 1 0 1 0

  11 - 20 dec

    0 0 0 0 1 0 1 1
    0 0 0 0 1 1 0 0
    0 0 0 0 1 1 0 1
    0 0 0 0 1 1 1 0
    0 0 0 0 1 1 1 1
    0 0 0 1 0 0 0 0
    0 0 0 1 0 0 0 1
    0 0 0 1 0 0 1 0
    0 0 0 1 0 0 1 1
    0 0 0 1 0 1 0 0

  91 - 100

    0 1 0 1 1 0 1 1
    0 1 0 1 1 1 0 0  
    0 1 0 1 1 1 0 1
    0 1 0 1 1 1 1 0
    0 1 0 1 1 1 1 1
    0 1 1 0 0 0 0 0
    0 1 1 0 0 0 0 1
    0 1 1 0 0 0 1 0
    0 1 1 0 0 0 1 1
    0 1 1 0 0 1 0 0     

  121 - 130

    0 1 1 1 1 0 0 1
    0 1 1 1 1 0 1 0
    0 1 1 1 1 0 1 1
    0 1 1 1 1 1 0 0
    0 1 1 1 1 1 0 1
    0 1 1 1 1 1 1 0
    0 1 1 1 1 1 1 1
    1 0 0 0 0 0 0 0
    wait a minute!
    That's negative


 == Overflow ==

You cannot store 130 in a single byte, because the possible values
for a single byte are between  -128 and +127 .

When the computer adds  127 + 1 in binary, the result is  10000000 .
That is actually negative :  -128.  So the computer thinks  127 + 1 = -128 !

When this happens, it's called an overflow .  

Overflows can also happen with bigger integers, even a 32-bit int.
The following Java commands suffer an overflow, printing the output shown.

      int a = 1000000000;
      int b = 2000000000;
      int c = a + b;
      output(a);
      output(b);
      output(c);
Output:
1000000000
2000000000
-1294967296

Practice questions:

(1) (a)  What is the largest integer that can be stored
               using 16-bit unsigned binary?    
               Give your answer using powers of 2.  ==>  2^16 - 1  

      (b)  How does this change if we use 16-bit two's complement?
               ==>  2^15 - 1

(2) Change each of the following unsigned binary bytes to decimal:
      (a)  00110011 bin = 51 dec    (b) 10101010 bin = 170 dec

(3) Change each of the following to two's complement binary:
      (a)  120 dec = 01111000 bin
      (b)  -127 dec = 10000001 bin
      (c)   -1  dec = -128 + 127 = 11111111 bin

(4) Add these binary numbers to get a binary number:
      (a)   00001011       (b)  00010101       (c)  01101010
          + 00001010           + 00111100          + 00011111
         ---------------          --------------        ---------------
             00010101              01010001             10001001         

(5)  How large is each of the following?
      (a) Java int variable = 32 bits  
      (b) a byte is  8  bits
      (c) a Java long variable is 64 bits

(6) If the computer adds two positive numbers together
     and the answer is too large for the available storage space,
     which of the following results:
     (a) an overflow occurs    ==>  true
     (b) the answer might be negative  ==>  true
     (c) the computer will crash   ==>  false
     (d) the compiler will prevent the program from running  ==> false

(7) IP addresses are stored as 32-bit integer values.
    (a) Approximately how many different IP addresses are possible?
         Give your answer both as a power of 2     ==>  2^32  (including 0.0.0.0)
          and as an approximate decimal number.  ==>  4 billion
    
(b) Which of the following IP addresses are valid?
             
192.168.207.35    yes valid   
             123.456.78.9        not valid  
             100.100.100.0      yes valid
          Explain why one of them is not valid.
            Each number represents an unsigned byte (8 bits),
             which has a maximum value of 255.  So 456 is not valid.

(8)  True color uses 24-bits to store the color of each pixel.
       How many different color values can be stored?
         power of 2 ==>  2^24          approximate decimal ==> 16 million