java Int binary principle with +, -, >>, >>>, ^, & computing

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/zhangyong01245/article/details/83715717

java Int binary principle with +, -, >>, >>>, <<, ^, & computing

    In a recent review of the basics of Java binary principle, launched a certain understanding, the first record of it, to facilitate follow-up study

Int Binary

  1. The default is int type symbol, the most significant bit 0 for positive, 1 for negative. Example:
      Binary 5: 0101;
      -5 binary: 1101;

  2. The original code, the inverted complement appreciated:
    A, original code: a positive number, in accordance with the absolute value of the converted binary number; converted according to a negative absolute value of a binary number, and then make up a bit, referred to as the original code For example:
      5 original code: 0101;
      -5 original code: 1101;

    B, anti-code: positive anti-code the same as the original code, negative inverse of the number of code symbols in addition to the original sign bit inverted you.
      Inverted 5: 0101;
      -5 inverted: 1010;

    C, Complement: complement positive number of the same original code, complement negative number in addition to the original code symbol bits Members inverted, and then the last one is increased by 1 (i.e., inverted + 1)
      5 complement: 0101;
      -5 complement: 1011;

  3. Main points:
    ① Positive anti-code and complement are the same as the original code.
    ② the negative inverse of the code number in addition to the original code symbol bit for your inverted.
    ③ complement negative number in addition to the original code symbol bits Members inverted, and then adding a last
    complement ③ complement of the original code

Int binary addition or subtraction

  1. Calculation Principle:
    A, + negative subtraction processed may be considered, so that the whole can be regarded as two numbers together subtraction

    B, generally in the form of binary complement sum is added, the final result is complemented code: X (complement) + Y (complement) = Z (complement); X-decimal converted to binary, Y represents a decimal turn into binary, Z represents a result of adding two binary sign bit is calculated, the number of bits exceeds the discard (such as X, Y a 3, a 5, the end result should be 5 Z, attention here relates to to overflow , it will be mentioned below).

    c, for example, 4 + 3 = 7 can be considered: 0100 + 0011 = 0111 (7)

    d, note that adding two numbers should remain the same binary bits, 0 is added padded bits after the sign bit is not enough.

  2. Binary addition overflow problems:
    A, calculated according to the principle (1) in spoken: when calculating (-6) + (-2) = -8 time: 6 in the original code -> anti-code -> Complement: 1110 -> 1001> 1010, 2 in the original code -> anti-code -> complement: 110-> 101-> 110, to maintain the same bit width and binary -6, and therefore the original code of 2 -> inverted - > complement: 1010-> 1101> 1110; two's complement sum of results: 1 1000, the median results than the original and more out of a leftmost 1 here, it will be natural discarded (is not a). Look result, complement code 1000 (in fact, it can be seen that it is 0). This is a far cry from what we want -8. The reason is that here there was overflow! ! !

    B, overrun is defined: -2-6 that the above example, i.e., 1 1 10 + 1 0 10 when two binary bits are added to each of the first (in red) are added into whichever bits (1 carry digit is 1 + 1) and each of the second number of bits added into bits (1 + 0 carry digit is 0) are compared, if not identical, an overflow occurs, the overflow occurs is determined -2-6, Conversely, an overflow does not occur
    , for example, the following binary calculation:
      1011 + 1111 -> into the top two bits: 1 and 0 -----> overflow;
      1111 +0111 -> into the first two digits: 0 and 1 - ----> overflow;
      1011 + 0101--> into the first two digits: 0 and 0 ------> not overflow;
      1111 + 0001 -> into the first two digits: 0 and 0 - ----> does not overflow;
    Note: the first two digits into: 10 indicates negative overflow, 01 denotes a positive overflow, we can go to understand the concepts and principles.

c, the solution when this happens:
  the binary -2 written 10010 (one more than at the start), - written in binary 6 10110. Performing complement Calculated:
     -2: original code -> anti-code -> Complement: 10010-> 11101-> 11110;
     -6: original code -> anti-code -> Complement: 10110-> 11001-> 11010;
     + 11010 = 11110 . 1 11000 -> 11000 11000 complement its corresponding bit: -8,
D, is calculated end

>>, >>>, << <<< expression of the meaning and operation principle:

  1. Java code is as follows:
public static void main(String[] args) {
    /**
     *  1、<< : 左移运算符:符号位不变,高位溢出,低位补0,对补码进行操作
     *  2、十进制--> 二进制补码 --->操作补码 --> 将补码取补码 -->十进制
     *  3、过程(二进制):
     *    a、6 的二进制补码(32位):00000000 00000000 00000000 00000110;
     *    b、向左偏移两位后补码:00000000 00000000 00000000 00011000;
     *    c、补码-->补码-->十进制:24
     *  4、打印结果为: 24
     */
    System.out.println("6 << 2 ===== " +(6 << 2));

    /**
     *  1、<< : 左移运算符: 符号位不变,高位溢出,低位补0,对补码进行操作
     *  2、十进制--> 二进制补码 --->操作补码 --> 将补码取补码 -->十进制
     *  3、过程(二进制):
     *    a、6 的二进制补码(32位):11111111 11111111 11111111 11111010;
     *    b、向左偏移两位:11111111 11111111 11111111 11101000;
     *    c、补码-->补码-->十进制:-24
     *  4、打印结果为: -24
     */
    System.out.println("-6 << 2 ===== " +(-6 << 2));

    /**
     *  1、>> : 右移运算符: 符号位不变,低位溢出,高位用符号位补高位,对补码进行操作
     *  2、十进制--> 二进制补码 --->操作补码 --> 将补码取补码 -->十进制
     *  3、过程(二进制):
     *    a、6 的二进制补码(32位):00000000 00000000 00000000 00000110;
     *    b、向左偏移两位:00000000 00000000 00000000 00000001;
     *    c、补码-->补码-->十进制:1
     *  4、打印结果为:1
     */
    System.out.println("6 >> 2 ===== " +(6 >> 2));

    /**
     *  1、>> : 右移运算符: 符号位不变,低位溢出,高位用符号位补高位,对补码进行操作
     *  2、十进制--> 二进制补码 --->操作补码 --> 将补码取补码 -->十进制
     *  3、过程(二进制):
     *    a、-6 的二进制补码(32位):11111111 11111111 11111111 11111010;
     *    b、向左偏移两位:11111111 11111111 11111111 11111110;
     *    c、补码-->补码-->十进制:-2
     *  4、打印结果为:-2
     */
    System.out.println("-6 >> 2 ===== " +(-6 >> 2));

    /**
     *  1、>>> : 无符号右移(无符号的意思是将符号位当作数字位看待): 低位溢出,高位补0,对补码进行操作
     *  2、十进制--> 二进制补码 --->操作补码 --> 十进制
     *  3、过程(二进制):
     *    a、6 的二进制补码(32位):00000000 00000000 00000000 00000110;
     *    b、向左偏移两位:00000000 00000000 00000000 00000001;
     *    c、补码-->十进制:1
     *  4、打印结果为:1
     */
    System.out.println("6 >>> 2 ===== " +(6 >>> 2));

    /**
     *  1、>>> : 无符号右移(无符号的意思是将符号位当作数字位看待): 低位溢出,高位补0,对补码进行操作
     *  2、十进制--> 二进制补码 --->操作补码 -->十进制
     *  3、过程(二进制):
     *     a、-6 的二进制补码(32位):11111111 11111111 11111111 11111010;
     *    b、向左偏移28位:00000000 00000000 00000000 00001111;
     *    c、补码-->十进制:15
     *  4、打印结果为:15
     */
    System.out.println("-6 >>> 28 ===== " +(-6 >>> 28));
    
}

Bit exclusive-OR operation (^), bitwise AND operator (&):

  1. Bit exclusive-OR operation (^):
    A, calculation rule is: into two binary numbers, and start from a high comparison, if the same was 0, 1 was not the same.
    B, for example: ^ 8 11: 8 is converted to binary 1011. 1000,11 is turned from the high order binary comparison is obtained: Then the binary to decimal 0011 is Integer.parseInt ( "0011", 2) = 3;

  2. Bitwise AND operator (&)
    A, computing rules: two numbers are converted to binary, and then start from relatively high, if the two numbers are 1, compared with 1, and 0 otherwise.
    b, such as: 129 & 128: 129 is converted into a binary 10000001,128 is converted to binary 10000000. From start comparing get high, get 10000000, which is 128.

Finally:
we have a better introduction to explore your comment, bloggers will promptly correct

Guess you like

Origin blog.csdn.net/zhangyong01245/article/details/83715717