java bit operation

The java bitwise operator
   is mainly for binary, which is to perform logical operations on the bits of two binary numbers.
1. And operator (bitwise AND)
    The AND operator is represented by the "&" symbol, and its operation rules are as follows: The result is 1 when both operands are 1, otherwise the result is 0. Such as:
public class Test1 {
    public static void main(String[] args) {
    	  int one = 1;
          int two = 2;
          System.out.println("The result of AND of one and two is: "+(one & two));
	}
}

The value of "one" is 1, which is 00000001 when converted to binary, and the value of "two" is 2, which is 00000010 when converted to binary. According to the operation rule of the AND operator, only if both bits are 1, the result is 1. It can be known that the result is 00000000, which is 0.
2. OR operator (bitwise OR)
    OR operator is represented by the symbol "|", and its operation rules are as follows: as long as one of the two bits is 1, then the result is 1, otherwise it is 0. Such as:
public class Test1 {
    public static void main(String[] args) {
    	  int one = 1;
          int two = 2;
          System.out.println("The result of OR of one and two is: "+(one | two));
	}
}

The value of "one" is 1, which is 00000001 when converted to binary, and the value of "two" is 2, which is 00000010 when converted to binary. According to the operation rule of the OR operator, as long as one of the two bits is 1, the result is 1, otherwise it is 0. You can know that the result is 00000011, which is 3.
3. NOT operator (bitwise NOT)
    The NOT operator is represented by the symbol "~", and its operation rules are as follows: if the bit is 0, the result is 1, and if the bit is 1, the result is 0. Such as:
public class Test1 {
    public static void main(String[] args) {
    	  int one = 1;
          System.out.println("one NOT operation is: "+(~one));
	}
}

The value of "one" is 1, and it is 00000001 when converted into binary. In the computer, the complement code is used for calculation. The original code, inverse code and complement code of positive numbers are all themselves, so the complement code of 1 is still 00000001. The complement code is 1111 1110 after bitwise negation, the highest bit is the sign bit, 1 represents a negative number, and 0 represents a positive number. So 1 is -2 after bitwise NOT.
Supplement: Excerpted from: http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
First of all, you need to understand a few concepts:
1.
    The binary representation of a number in a computer is called This number of machines. Machine numbers are signed, and the computer uses the highest bit of a number to store the sign, positive numbers are 0, and negative numbers are 1.
For example, the number +3 in decimal, the computer word length is 8 bits, and the conversion into binary is 00000011. If it is -3, it is 10000011. So, 00000011 and 10000011 here are the machine numbers.
2.

   Since the first bit is the sign bit, the formal value of the machine number is not equal to the real value. For example, the above signed number 10000011, the highest bit 1 represents negative, and its real value is -3 instead of the formal value 131 (10000011 converted to decimal equals 131). Therefore, for the sake of distinction, the true value corresponding to the machine number with the sign bit is called the true value of the machine number.
3. The original code

    The original code is the absolute value of the sign bit plus the true value, that is, the first bit represents the symbol, and the remaining bits represent the value. For example, if it is an 8-bit binary
[+1] original = 0000 0001

[-1] original = The first bit of 1000 0001

is the sign bit. Because the first bit is the sign bit, the value range of an 8-bit binary number is:

[1111 1111 , 0111 1111]

is [-127 , 127]
4. The inverse code of

a positive

number is the inverse code of its own negative number, which is based on its original code, the sign bit remains unchanged, and the other bits are inverted .

[+1] = [00000001] Original = [00000001] Inverse

[-1] = [10000001] Original = [11111110] Inverse
5, complement The complement of a

positive

number is itself the complement of a negative number is in its original code On the basis, the sign bit remains unchanged, the rest of the bits are inverted, and finally +1 . (That is, +1 on the basis of the complement code)

[+1] = [00000001] Original = [00000001] Inverse = [00000001] Complement

[-1 ] ] = [10000001] Original = [11111110] Inverse = [11111111] Complement

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942245&siteId=291194637