Bits and Integer operation

Computer as Binary Digital systems

  • A computer uses margins of error on voltage to determine what is an on and a off bit
    • 0v to 0.5v –> Binary zero
    • 0.5.1v - 2.399 –> Illegal
    • 2.4v to 2.9v –> Binary 1
  • Basic unit of information is a bit
  • values with more than two states require multiple states.
    • two bits has four possible states : 00, 01, 10, 11
    • three bits has eight possible states
  • A collection of n bits has 2^n possible states

Integers

  • Unsigned integers : 0 to 2^n-1 where n is the number of bits
  • Signed integers : -M to 0 and 0 to N where N-M < (2^n-1)

Converting unsigned integers

0  1  0  1  1
16 8  4  2  1

 1
 2
 8
+
---
11 subscript 10

You can use the leftmost bit to determine if a number is even or odd.

25 / 2 = 12 r 1 LSB (least significant bit)

12 / 2 = 6 r 0

6 / 2 =  3 r 0

3 / 2 =  1 r 1

1 / 2 =  0 r 1 MSB (Most significant bit)

Use the remainders from the MSB to the LSB
11001  

Unsigned binary numbers can have zeros added to the front without changing the number

Practice

10111
16+4+2+1 = 23

67
01000011

## Unsigned binary arithmetic
Similar conversion with extra steps

Base-2 additon

  • add from right to left, propogating carry
    10010
+    1001
----------
    11011

Unsigned Integer Arithmetic

Sum :

Sum:     Carry
1+1 = 0  1
1+1+1=1  1
0+1  =1  0
  10111
  10111
+
________

In the above case there is overflow. The result is more that 5 bits.

Signed Magnitude binary numers

The most significant bit determines whether its positive or negative

0 represents positive numbers 1 represents negative numbers

Cannot directly perform addition/subtraction. Two numbers for zero (+0 / -0)

Ones complement

A positive binary integer has a MSB of 0. A negative binary integer is represented by flipping each of its corresponding binary number

0101
// Invert to apply the ones complement
1010

Twos complement

Flip the bits and add a one to it.

0101
// Invert
1010
+  1 // Add one
------

This operation doesn’t require one to change sign, efficient use of all bits, Only one representation for zero

Twos complement and unsigned binary should be used.

Types of binary numbers

  • Unsigned binary (Normal binary), ub subscript : 1010011UB = 83b10
  • Signed magnitude (MSB 0 = positive, MSB 1 = negative ) sm subcript : 1010011SM = -19b10 note dont include the first
  • Ones complement (Flip the bits to see the other number ) 1c subscript : 1010011 = 0101100 = -44
  • Twos complement (Flip the bits and add one ) 2c subscript : 1010011 = 0101101 = 45

It is impossible for a 2s complement number to overflow. g

Twos complement subtraction

hardware adds negatives instead of subtracting 8 - 7 = 8 + -7

01000 = 8 00111 = 7 Applying twos complement: 11001 = -7

Now add

// Drop the carryover bit
 11
  01000 = 8b10
  11001 = 7b10
+ ______

  00001 = 1b10
0111_2c = 7_b10

Two complement exceptions

1000_2c

 1 // Drop this one
  0111
    1
  -----
1000_2c

This is an exception to twos complement. 1000 = -8 and there is no corresponding value. The same process applies to ```

Written on September 11, 2017