Created: 2023-10-13 12:40
Status: #idea
Subject: Mathematics Programming
Tags: Byte number Integer
Two's Complement
Two's complement is the most common method of representing signed integers on computers.
- it uses the first binary digit on the left to signify if it is positive
0
or negative1
. - This reduces the range of positive numbers that can be represented (using
bits, where is the number of bits) from to .
Converting Positive to Negative, Vice-versa
We invert all the bits, making the first bit
1
, then we add 1
to the new binary number.
- It also works vice-versa, from negative to positive.
Getting the Value of a Negative Binary
- to get
10011111
's value which is-97
, we represent the first bit as-128
orand then the other bits as positive numbers.
This then becomes -128 + (1+2+4+8+16) == -97
.