Binary Numbers: Basically, there are a lot of ways that we could count by using binary numbers - We've taken a look at three specific ways that we'll be using in various ways this semester. First, we need to establish that when we're talking about binary numbers in computers, we're always talking about a specific number of digits in the numbers. In this class, we'll be talking most about 32-bit numbers, but we may also occasionally talk about 16-bit and 8-bit numbers. Ex: 1000 1001b is an 8-bit binary number. This particular number could mean a lot of things. We can't really talk about it's meaning until we know more info. (For example: What type of a number is it? Unsigned? 2's Compliment?) Just like with decimal numbers, we may not always write out all the leading zeros on a number. Unsigned Numbers: Unsigned numbers are the "Natural" numbers. (The positive numbers that mankind has been using for ages to keep track of things like grain and livestock) Decimal: 0,1,2,...1000,...,10000,... etc. In binary, we just use the normal counting system for the unsigned numbers: 0b, 1b, 10b, 100b, 101b, 110b, BUT - we only have a fixed number of digits, so there's a limit to how high we can count. For example, if we are only able to work with 8-bit numbers, then we'd start counting at 0000 0000b, 0000 0001b, 0000 0010b, ... And finish with 1111 1111b. 1111 1111b has a decimal value of 255. That means that 8-bit binary numbers can only be used to represent numbers between 0 (0000 0000b) and 255 (1111 1111b). (This is one of the problems which we'll tackle later in the semester) Signed Numbers: Signed numbers include all the integers - the positives and the negatives. In computers, we can only work with 1's and 0's - we don't have a way to store a minus sign like we do when we work on paper. This is the reason why signed numbers in binary look awkward - we have to some how represent the sign with 1's and 0's. There are a lot of ways to do this, we'll take a look at the three most popular. Sign Bit: We designate one of the "digits" of the number as representing the "sign" - all other digits represent it's size. For example, with an 8-bit number, we might consider the left-most bit to indicate the sign (0=pos, 1=neg) and the remaining bits to represent the sign. Ex: 1000 1000b = -8 (If this were UNSIGNED, it would be 136) So, for our range is from: -127 (1111 1111b) to 127 (0111 111b) AND we have 2 values for zero: 0 (0000 0000b) and -0 (1000 0000). Actually, building a machine to do math with this form of representation is actually somewhat difficult, so it's only used for special cases like IEEE-754 floating point. Binary Offset: In binary offset, you just choose one of the "middle" numbers in your normal counting sequence to be zero. Counting up from that number gives you the positive numbers and counting down gives you the negative numbers. For our 8-bit numbers: If I choose 128 (1000 0000b) to be zero, then 0000 0000b is -128 ... 0111 1110b is -2 0111 1111b is -1 1000 0000b is 0 (by definition) 1000 0001b is 1 1000 0010b is 2 ... 1111 1111b is 127 So, we can come up with a simple formula for the conversion: To represent a number in binary offset: Take the decimal value of the number and add the decimal value of the "zero", then convert to binary. Ex: What is 2 in binary offset if the "zero" is 128 (1000 0000b): 2+128 = 130. 130 in binary is 1000 0010b. To convert a number to decimal from binary offset, just do the reverse: Take the decimal value of the number and subtract off the decimal value of the "zero". Ex: What is the decimal value of 0111 1110b, which is a binary offset number with the zero at 1000 0000b? 1. The decimal value of 0111 1110 is 126. 2. The decimal value of 1000 0000 is 128. 128-126 = -2 Again, this format is useful for IEEE-754 floating point, but it's difficult to build a machine that can perform math with these types of numbers. 2's Compliment: 2's compliment is the format that's really used in computers. The reason it's used is because it's easy to do math with both positive and negative numbers if they are represented in 2's compliment form. First, let's look at the basic rules for addition of numbers in decimal (let's assume that we're working with 4-digit numbers): What is 0199+9199? Answer: 0110 <- Carry digits 0199 (Notice the "carry in" of 0 to the right) +9199 (And the "carry out" of 0 to the left) ----- 9398 Notice that in order to add a multi-digit number, we only really need to be able to add 3 digits at a time. Binary works the same way, but sine we have fewer digits, we have a lot fewer cases to look at: 0 0 0 1 0 0 1 1 +0 +1 +1 +1 --- --- --- --- 0 0 0 1 1 0 1 1 (The left digit is the "carry out") (Remember that the order of the digits doesn't matter: 0+1+1 = 1+1+0 = 1+0+1) The thing that makes 2's compliment really nice is that we can use these basic rules to add both positive and negative numbers! (With the other formats, we get results that don't make sense if we use these rules) So, on to 2's compliment: 1) To convert a number to 2's compliment, first write it out as an unsigned binary number and a sign. 2) Make sure that the number uses the correct number of "bits" (If you have to "drop" any bits, then this isn't going to work!) 3) If the number is positive, check the "sign bit" (left most bit) if it's a one, this number is too big for 2's compliment! If the number is negative, "negate it". To negate a number just invert all it's bits and add 1b to it. (Shortcut: Find the rightmost one and invert all bits to it's left) NOTE: You can "negate" a number by inverting and adding 1. (This works both ways: makes a pos neg and makes a neg pos!) NOTE: The "positive" 2's compliment numbers are the same as the unsigned number. NOTE: The "leftmost" bit can be thought of as a sign bit: 0 means positive, 1 means negative. Examples: Express -2 as 4-bit 2's compliment: 1) -2 = -10b 2) -2 = -0010b 3) -0010b => 1101b+1b = 1110b Express -2 as 8-bit 2's compliment: 1) -2 = -10b 2) -2 = -0000 0010b 3) -0000 0010b => 0000 1101b + 1b = 1111 1110b Express 5 as 8-bit 2's compliment: 1) 5 = 101b 2) 5 = 0000 0101b 3) 0000 0101b! Express -5 as 8-bit 2's compliment: 1) -5 = 101b 2) -5 = -0000 0101b 3) -0000 0101b => 1111 1010b + 1b = 1111 1011b Now, as mentioned earlier, the real advantage of this format is that we can follow our four simple addition rules for perform all addition/subtraction! (Remember subtraction is just adding a negative) Ex: Show the full addition of 5+-2 with 8-bit 2's compliment numbers: A) We've already found 5 as a 8-bit 2's compliment number: 0000 0101b B) We've already found -2 as a 8-bit 2's compliment number: 1111 1110b C) Addition: 1111 1 <- Carry Bits 0000 0101b +1111 1110b ----------- 1 0000 0011b = 0000 0011b (We only have 8-bit numbers) 5+-2 = 0000 0011b = 3! Work Checks!!! Ex: Show the full subtraction of 5-7 with 8-bit 2's compliment numbers: A) We've already found 5 as a 8-bit 2's compliment number: 0000 0101b B) Find 7 as a 2's compliment number: 1) 7 = 111b 2) 7 = 0000 0111b 3) Done! 7=0000 0111 C) Subtraction: 0000 0101b -0000 0111b ----------- We don't have subtraction rules!!!! Make the problem into addition: 0000 0101b + (-0000 0111b) --------------- What is -0000 0111b? Just take the 2's compliment: -0000 0111b => 1111 1000b + 1b = 1111 1001b Now we can finish the problem: 1 <- Carries 0000 0101b +1111 1001b ----------- 1111 1110b Let's check the work - what's the value of 1111 1110b (which is an 8-bit 2's compliment number)? There are two ways to find out: A) The power of 2 method: -2^7+2^6+2^5+2^4+2^3+2^2+2^1 = -2! (Work checks) B) Find the sign/magnitude: The left bit is a 1 so it must be negative. To find the magnitude make it positive: -1111 1110b => 0000 0001b + 1b = 0000 0010b = 2 Therefore we have a -2! (Work checks) Again, this is the real power of 2's compliment numbers. We can perform addition and subtraction (as well as work with positive and negative numbers) with only our four simple addition rules!!! (So, later in the semester, we only have to build a machine that can follow there four rules in order to build a computer capable of addition and subtraction!!!) Try addition and subtraction with the other number formats mentioned - you'll find that the results don't make sense unless you use a lot of special rules.