![]() As I showed before addition between signed and unsigned values works normally with no issues. Should you use unsigned or signed? The problem here is that the CPU doesn't know whether what's in it's registers is signed or unsigned, it simply has a pattern of bits: 1001 (assuming 4 bit registers) could be the value 9, -7, or a bitmask in which case it doesn't represent a number at all. Now one thing that really confuses people is how this works when implementing a CPU. ![]() Basically anything you need to be able to deal with negative values. Or if you are calculating the FFT of a signal, or. You might want to rotate the vector: 1,0 by 180 degrees, at which point the result would be -1,0. For example if you are doing vector rotations. If you need signed numbers then use signed. ![]() You don't need to represent negative numbers, so use unsigned. For example if you implement a counter that counts from 0 to 9. If you can guarantee the value will only ever be positive, then use unsigned. Both are a range of 16 values, we just choose where we want to start.Īnd in what situations you would use one/the other/both? Note that in C/C++ you have int and unsigned int.įinal comment, note that with 4 bits you can represent 16 unique values: 0000 - 1111. Which is why we distinguish between signed numbers (they have a sign, aka +/-) and unsigned numbers (only ever positive). This is fine if you need to use both negative and positive numbers, but is inefficient if you only ever use positive numbers (or only ever use negative numbers). But if we want to support negative numbers we need to use an additional bit, because otherwise we wouldn't be able to distinguish between +9 (1001) and -7 (1001). If we ignored negative numbers we can represent 9 as a 4 bit number: 1001. If we do the NOT, +1 again, we get 0_1100, so the result is -12, as expected.Īnyway, that's why we use two's complement. Without going into too much detail this makes life easier for computers to deal with. Then that gives us the value 14, and because we were initially negative, it's -14. Then for positive numbers we treat the rest as normal, so we have +2.ġ_0010: now because it's a negative number, we take the NOT of the number: 0_1101, and we add 1: 0_1110. Where the left most digit represents the sign still, but the rest of the number is a bit different.Ġ_0010: as before the left most 0 means it's positive. The most common method of doing this is to use 2s complement. Note however this is how floating point works. This is not great because it means we have two values for 0: 0_0000 and 1_0000, or +0 and -0, which makes life a bit more complicated. One way is just to say the left most digit is the sign bit. So how do we distinguish the sign from the number. When we represent numbers in binary we need something similar, but we can't just add a + or a -, we are using binary, we have 0 or 1 that's it. When we write numbers on paper we use an extra symbol to indicate if it's a positive or negative number.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |