Beau Schwabe
Active Member
Ok, so I had a REALLY dumb epiphany and I just want to make sure I'm not missing anything.
To make things simple I'll use 4 bits.
I have a number that ranges from 7 to -8 with 0 or -1 being center.
I want to convert that to a number that ranges from 0 to 15 with 7 or 8 being center.
In the past I would check the MSB; if it was a "0" then I would ADD 8; if it was a "1" I would SUBTRACT 8 ... without really even thinking what was going on.
Looking at this closer, all I need to do is invert the MSB and I'm done. No adding or subtracting required. Am I understanding that correctly? Even up to 32 Bit numbers?
Going in reverse, that might make for a really slick way to generate a 2's complement output.
To make things simple I'll use 4 bits.
I have a number that ranges from 7 to -8 with 0 or -1 being center.
I want to convert that to a number that ranges from 0 to 15 with 7 or 8 being center.
In the past I would check the MSB; if it was a "0" then I would ADD 8; if it was a "1" I would SUBTRACT 8 ... without really even thinking what was going on.
Looking at this closer, all I need to do is invert the MSB and I'm done. No adding or subtracting required. Am I understanding that correctly? Even up to 32 Bit numbers?
Going in reverse, that might make for a really slick way to generate a 2's complement output.
Code:
2's Complement: Desired Output:
0000 = 0 8
0001 = 1 9
0010 = 2 10
0011 = 3 11
0100 = 4 12
0101 = 5 13
0110 = 6 14
0111 = 7 15
1000 = -8 0
1001 = -7 1
1010 = -6 2
1011 = -5 3
1100 = -4 4
1101 = -3 5
1110 = -2 6
1111 = -1 7