Number Systems
Binary
Binary consists of numbers 0 and 1. Each digit represents 32, 16, 8, 4, 2, 1 from right to left. I.e powers of 2.
Convert
solution
Convert
solution
Keep dividing by 2 until the quotient is 0, keeping track of the remainder.
Number | Quotient | Remainder |
---|---|---|
38 | 19 | 0 |
19 | 9 | 1 |
9 | 4 | 1 |
4 | 2 | 0 |
2 | 1 | 0 |
1 | 0 | 1 |
The number is
Signed Integers
A sign bit is the first (leftmost) bit of a binary number. 0 indicates a positive, 1 indicates negative.
First bit, aka most significant bit (msb)
NB: msB means most significant byte
Sign and Magnitude
So he [Anwar Hasan] taught us sign and magnitude because it's shit. Ok.
- Kegan
Use a sign bit at the front, and the rest of the number is magnitude.
is is
- Sign and magnitude system has two zeros: -0 and +0
- Bad because we can't do arithmetic with it
One's Complement
Use a sign bit at the front, and then take the complement of all the other bits.
is is
- One's complement system has two zeros: -0 and +0
- Also hard to do arithmetic with
Two's Complement Form
AKA signed two's complement (S2C)
Use a sign bit at the front, take the complement of all the other bits, and add 1.
is is
- Two's complement only has one 0
- It's very easy to do arithmetic in two's complement form; you just treat it as a normal number
Let
Computers implement arithmetic mod
- Take 1's complement, then add 1
- Complement the bits to the left of the rightmost 1
To go backwards, interpret the sign bit as
Arithmetic
Addition and subtraction are easy in two's complement form. Add two numbers as you normally would. To subtract two numbers, just take two's complement of the other number and add it.
We know in base 10,
Discarding the bit on the left, we have
Integer Overflow
Range of
- Unsigned:
- Signed:
Overflow occurs when an operation result exceeds the range
For
Detecting overflow: both operands have the same sign and the result is a different sign
You can implement this boolean expression as a SOP.
Sign Extension
To increase the "space" of the integer, replicate the msb
Shifting
todo Change formatting with underbraces, also note that this should be for multiplication and division, and the techniques are shifting
Shift left
Shift right
Octal
Octal involves digits
Convert
solution
Convert
solution
Keep dividing by 8 until the quotient is 0, keeping track of the remainder.
Number | Quotient | Remainder |
---|---|---|
420 | 52 | 4 |
52 | 6 | 4 |
6 | 0 | 6 |
The number is
Hexadecimal
Hexadecimal involves digits
Convert
solution
Convert
solution
Keep dividing by 16 until the quotient is 0, keeping track of the remainder.
Number | Quotient | Remainder |
---|---|---|
109 | 6 | 13 (D) |
6 | 0 | 6 |
The number is
Converting Between Number Systems
Other than base 10.
Octal and Binary
Octal to Binary: replace each octal symbol by its 3-bit binary representation
Binary to Octal: start from the least significant end (right), and group 3 binary bits into their corresponding octal symbol
Convert
solution
Convert
solution
Hex and Binary
Hex to Binary: replace each hex symbol by its 4-bit binary representation
Binary to Hex: start from the least significant end (right), and group 4 binary bits into their corresponding hex symbol
Convert
solution
Convert
solution