Primitive Code, Inverse Code, Complement Code and Bit Operation

Keywords: Java calculator

Directory Guidance:

  I. Original Code, Counter Code and Complement Code

  Bit operation

I. Original Code, Counter Code and Complement Code

There is only an adder in the computer. The data stored and calculated in the calculator are all complements. The positive number is the same as the zero's original code, inverse code and complement, while the negative number's original code, inverse code and complement are different.

Original code: symbol bit + absolute value (0 is positive, 1 is negative)

Inverse codes: the symbolic bits remain unchanged and the remaining bits are reversed

Complement: countercode + 1

1. Why use complement storage and complement calculation?

Because of the problems of + 0 and - 0 and errors in the computation of the original code and the inverse code, the complement code is correct and simple, and the symbol bits are directly involved in the calculation.

Examples:

Int is 4 bytes, 1 byte is 8 bits, so an int value is 32 bits, the first bit is the symbol bit, so the range of int value is - 2 ^ 31 ~ 2 ^ 31 - 1 (1000000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 0111111111 11111 11 11 11, the binary is complement)

2+(-1) and 1+(-1) are calculated respectively. The procedure is as follows:

 

2. The original code, counter code and complement codes are 00000000 00000 00010, 00000000 00000 00010, 00000 00000 00010, 00000000 00000 00000 00010, 00000000 00000 00000 00000 00000010, 00000 00000 00000 000000 00010, respectively.

The original code, inverse code and complement code of 1 are 00000000 00000 00000001, 00000000 00000 00000001, 00000000 00000 00001, 00000000 00000 00000 00000001, 00000000 00000 00000 00000001, 00000 00000 00000 00000 00000001, respectively.

- The original code, inverse code and complement of 1 are 10000000 00000 00001, 11111111111 11111 11111 11111 11110, 11111111111 11111 11111 11111 11111 11111 11111 11111 respectively.

 

The result of adding 2 and 1 primitives is: 100000000000000011 (primitive code), 0 is positive, 1 is negative, so the value is - 3, error.

The result of adding 2-1 inverse codes is: 00000000000000000000 (inverse codes), the result of corresponding original codes is 00000000000000 (original codes), the value is 0, the error.

The result of adding 2-1 complement is: 00000000000000000001 (complement), and the result of corresponding original code is 00000000000000001 (original code), the value is 1, which is correct.

 

The result is: 10000000 00000 00010 (original code), the value is - 2, error.

The result is 11111111111 11111 11111 11111 11111111 11111 11111 11111 11 11 11111 (inverse code). The result of the corresponding original code is 1000000 000 000 000 000 000 000 000 000 000 000 (original code), the value is - 0, which is inaccurate. (About the design of + 0 and - 0, you can Baidu if you are interested)

The result is: 00000000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 00 corresponding to the original code, the value is 0, correct.

Bit operation

Bit operators include: and (&), or (|), non (~), exclusive (^), left (<), right (>), unsigned right (>).

& When the binary bit is 1 at the same time, the result is 1, otherwise it is 0.

| When the bit has a 1, the result is 1, otherwise it is 0.

~ Bit 0 changes 1, 1 Changes 0

^ When the bit is different, the result is 1, otherwise it is 0.

<< <: Bit moves left as a whole, and right complements 0.

>> Bits move to the right as a whole, positive left complement 0, negative left complement 1

> ">: The position moves to the right as a whole, and the left side is filled with 0.

Examples:

public class BitOperationTest {

public static void main(String[] args) {
int a = 13, b = 6;
System.out.println(" a : " + getBinaryStr(a));
System.out.println(" b : " + getBinaryStr(b));
System.out.println(" a&b : " + getBinaryStr(a & b));
System.out.println(" a|b : " + getBinaryStr(a | b));
System.out.println(" ~a : " + getBinaryStr(~a));
System.out.println(" a^b : " + getBinaryStr(a ^ b));
System.out.println(" a<<b : " + getBinaryStr(a << b));
System.out.println(" a>>b : " + getBinaryStr(a >> (b - 4)));
System.out.println(" -a : " + getBinaryStr(-a));
System.out.println(" a>>>b: " + getBinaryStr(a >> (b - 4)));
System.out.println("-a>>>b: " + getBinaryStr((-a) >> (b - 4)));
}

private static String getBinaryStr(int n) {
StringBuilder str = new StringBuilder(Integer.toBinaryString(n));
int len = str.length();
if (len < 32) {
for (int i = 0; i < 32 - len; i++) {
str.insert(0, "0");
}
}
return str.substring(0, 8) + " " + str.substring(8, 16) + " " + str.substring(16, 24) + " " + str.substring(24, 32);
}
}

//Result 
   a    : 00000000 00000000 00000000 00001101
   b    : 00000000 00000000 00000000 00000110
  a&b   : 00000000 00000000 00000000 00000100
  a|b   : 00000000 00000000 00000000 00001111
   ~a   : 11111111 11111111 11111111 11110010
  a^b   : 00000000 00000000 00000000 00001011
  a<<2  : 00000000 00000000 00000000 00110100
   -a   : 11111111 11111111 11111111 11110011
  a>>2  : 00000000 00000000 00000000 00000011
(-a)>>2 : 11111111 11111111 11111111 11111100
  a>>>2 : 00000000 00000000 00000000 00000011
(-a)>>>2: 00111111 11111111 11111111 11111100

 

 

If you have any questions, you are welcome to raise them. If you have any mistakes, you are welcome to correct them.

For reprint, please specify the address of this article: https://www.cnblogs.com/yqx1116/p/11657307.html

Posted by wolf on Sun, 13 Oct 2019 01:45:33 -0700