Saturday, June 25, 2022

To Binary

 I have long heard that computers subtract by addition.

It's actually rather clever. The most significant bit (in a signed number), on

the left, is the sign of the number: 0 iindicates a positive number,

1 a negative. So if the second (negative) number in our operation

 is greater than the first (positive) one, the 0 flips to 1 and one has

a negative value.


So why do we say that it is subtraction through addition: because one is

adding a positive number and a negative one, rather then subtracting

a positive from a positive. This magic works as we flip the second number,

transforming a small positive number into a lage negative one. ie

a simple 8-bit binary number represents numbers of 0 to 255, while a flipped

number goes from 0 to 127 in the positive and 0 to -127 for the negative.

Problem here, 0 and -0 have different representations. Adding 1 to the

flip solves the problem. One then subtracts by addition with no problem.




Two's complement is standard for most computers. Below, (4 - 1).

0000 0100

1111 1111

________

0000 0011

So essentially, the machine just adds 0s and 1s, which we can emulate

using the normal rules of addition ie with a carry-over!!


Binary Subtraction - Rules, How to do Binary Subtraction, Binary Subtraction using 1's complement, Examples. (cuemath.com)

Interesting site:

Binary and Decimal Numbers - AndyBargh.com


No comments: