Back
Next
Numeric representation
 
  Binary values can be represented as powers of 2

    2^0 = 1

    2^1 = 2

    2^2 = 4

    2^8 = 256

    2^10 = 1024 or 1 Ki (Kilo)

    2^16 = 65536 (65.536 K) or  2^6 * 2^10 = 64 * 1Ki = 64 Ki

    2^20 = 2^10 * 2^10 =  1024 * 1024 or 1 Mi  (Mega)

    2^30 = 1 Gi (Giga)

    2^32 = 2^2 * 2^30 = 4 * 1 Gi  = 4 Gi. 
  
  K (decimal) vs. Ki (binary) 65KB vs. 64KiB

Using the same placeholder technique used with decimal numbers.
Position numbered from the right, the least significant bit is bit 0.

And it's position is also it's power.

000
111
1022+0
1132+1
10044+0+0
10154+0+1
11064+2+0
11174+2+1
Techniques for converting between binary and decimal.
  Divide by 2

  To convert decimal to binary.

    While number > 0
      Divide number by 2
      Set aside remainder
    End_While

    Read string of remainders in reverse order.

  To convert binary to decimal. 
  
  Multiply by 2 (shift left) and add

    Sum = 0.
    Take left most bit and add to sum.
    Discard left most bit.

    While more bits
      Multiply sum by 2 (shift left) 
      Add left most bit to sum.
      Discard left most bit.
    End_While
  
   477
   238   1  LSB  number of   1s
   119   0       number of   2s
    59   1       number of   4s
    29   1       number of   8s
    14   1       number of  16s
     7   0       number of  32s
     3   1       number of  64s
     1   1       number of 128s
     0   1  MSB  number of 256s

When using divde by 2,
Remember to invert column

  111011101

  # A word on padding. 

    When working with unsigned binary, it is unneccesary to left pad with zeros
    unless you need to be aware of working storage size for other reasons..
   
  e.g  0000 0001 1101 1101   # number above displayed as 32 bit word.

 Initial    Resulting
   value          sum
              
       0    
  1    0*2 + 1 =    1
  1    1*2 + 1 =    3
  1    3*2 + 1 =    7
  0    7*2 + 0 =   14
  1   14*2 + 1 =   29
  1   29*2 + 1 =   59
  1   59*2 + 1 =  119
  0  119*2 + 0 =  238
  1  238*2 + 1 =  477
Alternative.
1 2 4 8 16 32 64 128 256 512
 Subtracting powers of 2.

  477
- 256 x 1   2^8  MSB
  221
- 128 x 1   2^7 
   93
-  64 x 1   2^6
   29
-  32 x 0   2^5
   29 
-  16 x 1   2^4
   13
-   8 x 1   2^3
    5
-   4 x 1   2^2
    1
-   2 x 0   2^1
    1
-   1 x 1   2^0  LSB
    0
  Adding powers of 2

   1  x 2^8 (256) = 256
   1  x 2^7 (128) = 128  384
   1  x 2^6  (64) =  64 
   0  x 2^5  (32) =   0   64  448
   1  x 2^4  (16) =  16
   1  x 2^3   (8) =   8   24      477
   1  x 2^2   (4) =   4   
   0  x 2^1   (2) =   0    4   29
   1  x 2^0   (1) =   1    1