I'm trying to add up two 8 bit numbers which one is negative and second one is positive. Here it is what I do:
92-113
so I represent each number as binary
92 - 01011100
113 - 01110001
after changing 0 to 1 and 1 to 0 I get :
10001110 and after adding 1 I have 1000111 which is -113
then I'm adding them up and I get :
11101011
what totally makes no sense, what I probably do wrongly ? Would really love to know where I make mistake as it's really basic knowledge ;/
You're not missing anything - 11101011
is the binary equivalent of -21 (92-113) in 8-bit signed binary.
For singed integer types, the left=most bit determine if the number is positive or negative. To get the additive inverse, you flip all bits but the rightmost. It;s the same process you used to convert 113 to -113.
Doing that yields 00010101
which is 21. So 11101011
is -21.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.