Page 1 of 1
An overview of and intro to signed numbers
3 Replies - 15561 Views - Last Post: 28 November 2010 - 12:47 PM
Posted 25 November 2010 - 03:21 PM
Well, usually unsigned numbers are talked about. These are all assumed to be positive numbers. Signed numbers include negative numbers.
The HO (high order) bit determines the sign of a number. If the HO bit is 0 then the number is positive. If the HO bit is a 1, the number is a negative.
If we have the binary number 01101001 we know that it is a positive number just by looking at the HO bit. In the same way, we know that the number 10100110 is a negative number.
Now, this is only true because the numbers above are able to be broken into sections of four. If we had the number 111001, is it positive or negative? Aha, it's actually a positive number. If we split the number into sections of four we get 11 1001. Well we need two more digits. Those will be zeros in front so we get 0011 1001. You always need to fill the number to be broken into sections of 4 to know if the number is negative or positive.
Finding A Number's Inverse
Let's switch some numbers from negative to positive and from positive to negative.
If we have the number 5 in a byte we get 00000101. We'll split it into sections of four for easy viewing. 0000 0101 is our number now. So now we have to reverse the bits which is the same thing as applying the logical not operator to the number. We'll get into that later. By reversing all the bits we go from 0000 0101 to 1111 1010. Now this equals -6. Then we just add one to our number and get -5. Now let's do it in reverse. We start with -5 which is 1111 1011. Now we reverse the bits to get 0000 0100. Now we add 1 and get 0000 0101. Hey! That equals 5!
Sign Extending/Contracting Numbers
Sometimes we need to compare two different sized values. What if we wanted to compare a number that is a byte with a number that is a word? Well, fortunately that is very easy to do.
Let's take the hexadecimal number F8. 1111 1000 is our number. We can see that the HO bit is 1. We can just copy the value in the high order bit into the values we need to fill to. If we want to take our number and compare it with a word, we can take the 1 and fill. We end up getting 1111 1111 1111 1000 for our number. This equals FFF8 in hexadecimal. In the same way, if we have 20C9, we can take the HO bit which is 0 and copy it into the other bits. We end up getting 000020C9 to compare with our word.
To extend an unsigned number, you would just copy 0 into all the bits you need to fill.
Well say we have a number as a word and we want to fit it into a byte. Can we do that? Well, sometimes we can. If our number's HO byte(s) that we want to take off are all 0 or 1 (00 or FF in hexadecimal) then we can contract it!
Let's take FFF8. Now, we can fit that word into a byte because it's HO byte is FF. We just take off the leading byte's FF and end up with F8.
Determining Negative Number
Alright well we know how to convert unsigned numbers that we assume to be positive into decimal values. How do we convert signed numbers? Well, if the number is positive you already know how. If the number is negative, it's not too difficult.
Say we have the number 50. That equals 0011 0010. What if we make it negative by putting a 1 in the HO bit so we get 1011 0010? What number do we have now? Well, it's not as simple as just putting a negative sign in front because 1011 0010 doesn't equal -50. We need to determine what the value of the number would be if our entire number was 1000 0000. In this case, we take 1x2^7 and get 128. Since this is a negative number as seen in the HO bit we have -128. That isn't our answer though. Now we take our answer from that and add our original number. In this case, we just got -128 so we're going to add 50. -128 + 50 = -78. That's our answer right there.
That's it for this tutorial. Hopefully you've learned something new about numbers here. If you have any questions, feel free to ask them.
Replies To: Signed Numbers
Re: Signed Numbers
Posted 26 November 2010 - 06:46 AM
Also, what use could expanding and contracting a number be?
Re: Signed Numbers
Posted 26 November 2010 - 07:26 AM
About contracting and expanding: What do you do if you have a byte that you need to compare to a word? Well, you sign extend it. What do you do if you have a dword that you need to compare with a word that is contractible? You contract it.
This post has been edited by HiddenDragon: 26 November 2010 - 07:37 AM