Binary “There are 10 types of people in the world: Those who understand binary, and those who don't.”
What is Binary? Binary is the base-2 numeral system. We normally use the base-10 system. The only symbols in binary are “0” and “1”. “0” represents no electron flow, aka “OFF”. “1 “ represents electrons flowing, aka “ON”.
Bits and Bytes Each digit in a binary number is a bit. Eight bits make up one byte.
Base-10 versus Base-2 BASE 10 100 = 1 101 = 10 102 = 100 103 = 1000 Base-10 uses ten as the base of the exponent, while Base-2 uses a two for the base. BASE 10 100 = 1 101 = 10 102 = 100 103 = 1000 BASE 2 20 = 1 21 = 2 22 = 4 23 = 8
Converting Binary Values Add up all of the place values that hold a “1”. Place Values: 8’s, 4’s, 2’s, 1’s 0101 = 4 + 1 = 5 1110 = 8 + 4 + 2 = 14 0111 = 4 + 2 + 1 = 7
Adding Binary Numbers 0 1 0 1 . . . . . . 5 + 0 0 0 1 . . . . . . +1 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 . . . . . . carry the left “1” 1 + 1 + 1 = 11 . . . carry the left “1” 0 1 0 1 . . . . . . 5 + 0 0 0 1 . . . . . . +1 ================= 0 1 1 0 . . . . . . 6
What is an Overflow? 1 1 1 1 = 15 + 0 0 0 1 = + 1 ============ Overflow occurs when there is a carry into a column that doesn’t exist! 1 1 1 1 = 15 + 0 0 0 1 = + 1 ============ 0 0 0 0 = 0?
Unsigned versus Signed An unsigned binary number can only be positive and has all of it’s bits calculated into the size of the value. A signed binary number can be positive or negative. The left most bit determines the sign. “0” denotes positive, while “1” denotes negative. Unsigned 0 1 0 1 = 5 1 0 1 0 = 10 1 1 0 1 = 13 Sign&Mag 0 1 0 1 = 5 1 0 1 0 = -2 1 1 0 1 = -5
Problems with Sign&Mag There are two zeros: 0000 (+0) and 1000 (-0). It is possible to have a carry into the “sign bit” by accident, causing errors in math! 0 1 1 1 = 7 + 0 0 1 0 = 2 ============= 1 0 0 1 = -1? Should = 9
One’s Compliment 0 0 0 1 = 1 1 1 1 0 = -1 0 1 1 0 = 6 1 0 0 1 = -6 Flip all of the bits and call that negative! If you have a carry out from sign bit during addition, do an end-around carry! This still has two zeroes: 0000 and 1111. 0 0 0 1 = 1 (flip for negative) 1 1 1 0 = -1 0 1 1 0 = 6 (flip for negative) 1 0 0 1 = -6
Two’s Compliment 0 0 0 = 0 flip+1 maps to 0 0 0 Flip all of the bits, then add one, call it negative! Do not do an end-around-carry! 0 0 0 = 0 flip+1 maps to 0 0 0 0 0 1 = 1 flip+1 maps to 1 1 1 0 1 0 = 2 flip+1 maps to 1 1 0 0 1 1 = 3 flip+1 maps to 1 0 1 1 0 0 = -4 given to negative side 1 0 1 = -3 from mapped +3 1 1 0 = -2 from mapped +2 1 1 1 = -1 from mapped +1
Range of a Binary Number It depends on: Number of Bits System: Unsigned, Sign&Mag, One’s, Two’s 4-bit Unsigned can go from: 0 0 0 0 = “0” 1 1 1 1 = “15” 4-bit Signed in Two’s Compliment can go from: 1 0 0 0 = “-8” 0 1 1 1 = “7”