In binary and decimal processing, sometimes small tricks are useful.
1. Convert decimal number to binary number
(1) in MATLAB, there is a function dec2bin, you can convert a positive integer to 2 binary
(2) For negative numbers there is a conclusion: n-bit binary negative X's complement corresponding to the unsigned number is 2N +x
Example: Signed original code: 10,010 binary:-1
Anti-code: 1110
Complement: 1111 unsigned number: 15
15 = 24 + (-1);
This allows for quick knowledge of the 4-bit binary representation of-5 's complement of unsigned numbers of 16-5=11 that is 1011
2. Convert negative binary complement to decimal
(1) The sign bit is removed, and the remaining bits are reversed plus one, the resulting number is the absolute value of the binary complement of negative numbers.
(2) directly to the two complement to take the opposite plus one;
(3) 1011 (-1) *23+0*22+1*21+1*20 = 5
Problem:
Some tricks in binary processing