introduce
Binary numbers can be divided into signed numbers and unsigned numbers, and signed numbers can be divided into original code, inverse code, and complement code.
What is a sign?
It is to identify positive and negative numbers, representing 0 as positive and 1 as negative at the beginning and end. Unsigned is the opposite.
Original code, inverse code, complement code
The original code, inverse code, and complement code of positive numbers are all the same, and there is nothing to talk about.
negative number
The original code of the negative number, the highest bit is 1, the complement code is the inverse of the original code except the highest position, and the complement code is the complement code plus 1.
Example:
Decimal | original code | one's complement | complement |
---|---|---|---|
1 | 0000 0001 | 0000 0001 | 0000 0001 |
-1 | 1000 0001 | 1111 1110 | 1111 1111 |
binary arithmetic
Mainly explain the operations of binary addition, subtraction, multiplication and division
Addition: Every two goes into one.
0+0=0,0+1=1,1+0=1,1+1=10
Subtraction: Borrowing one from a high position is equivalent to two.
1-1=0,1-0=1,0-0=0,0-1=1
Multiplication: The result is "1" when both are "1"
0×0=0,0×1=0,1×0=0,1×1=1
Division: Similar to division of decimal numbers, except that the result is only 0 and 1.
In addition, regarding the relationship between multiplication and shifting,
left or right shift is the operation of binary numbers. In order to facilitate our calculations, we associate it with multiplication.
Shifting a binary number to the left by N bits is the decimal number corresponding to the binary number multiplied by 2 to the power of N.
Shifting right by N bits is a decimal number divided by 2 to the power of N.
Such as: binary number 0000 0100 (4) left shift 2 bits is 0001 0000, which is 4X2^2=16