Summary of Java bit operations (commonly used in LeetCode)

Java bit operations are binary shift operations for integer data types. It mainly includes bit AND, bit OR, bit NOT, signed left shift, signed right shift, unsigned right shift, etc.

In computer systems, values ​​are always represented and stored in two's complement numbers. The reason is that, using the complement code, the sign bit and the value field can be processed uniformly; at the same time, addition and subtraction can also be processed uniformly. In addition, the complementary code and the original code are converted to each other, and the operation process is the same, and no additional hardware circuit is required.

    1.求负整数的补码,将其对应正数二进制表示所有位取反(包括符号位,0变1,1变0)后加1

The 0's complement representation of a number is unique

    2.转化为原码
已知一个数的补码,求原码的操作其实就是对该补码再求补码:
⑴如果补码的符号位为“0”,表示是一个正数,其原码就是补码。
⑵如果补码的符号位为“1”,表示是一个负数,那么求给定的这个补码的补码就是要求的原码。

【例】已知一个补码为11111001,则原码是10000111(-7)。
因为符号位为“1”,表示是一个负数,所以该位不变,仍为“1”。
其余七位1111001取反后为0000110;
再加1,所以是10000111。

PS: There is no unsigned left shift; the
computer indicates that the positive and negative numbers are not represented by + - plus and minus signs, but by the highest digit, 0 means positive, 1 means negative


  1. Number of bytes occupied by Java basic data types

            数据类型       字节数     默认值
            byte          8bit              
            boolean       8bit     false
            char          8bit        
            short         16bit      0
            int           32bit      0
            long          64bit      0L
            float         32bit      0.0f
            double        64bit      0.0d
    
  2. bit operation

        位与&  (真真为真,真假为假,假假为假)
        位或 |   (真真为真,真假为真,假假为假)
        位非 ~  (取反码)
        位异或 ^ (真真为假,真假为真,假假为假)  相同则为假
        有符号左移:按二进制形式把所有的数字向左移动对应的位数,高位移出(舍弃),低位的空位补零。
    
        有符号右移>>(若正数,高位补0,负数,高位补1)
        -4>>2
            1111 1111 1111 1111 1111 1111 1111 1100   补码
            1111 1111 1111 1111 1111 1111 1111 1111   右移,最左边空出两位按规则负数空位补1
            1000 0000 0000 0000 0000 0000 0000 0000   解码
            1000 0000 0000 0000 0000 0000 0000 0001   补码(补码即最后一位+1)
            结果:-1
                    无符号右移:高位的空位补零
    
  3. Applications of bit operations

    1).  判断int型变量a是奇数还是偶数    
             a&1  = 0 偶数 
             a&1 = 1 奇数
    2).  求平均值,比如有两个int类型变量x、y,首先要求x+y的和,再除以2,但是有可能x+y的结果会超过int的最大表示范围,所以位运算就派上用场啦。
            (x&y)+((x^y)>>1); 
    3.  对于一个大于0的整数,判断它是不是2的几次方
            ((x&(x-1))==0)&&(x!=0); 
    4.  比如有两个int类型变量x、y,要求两者数字交换,位运算的实现方法:性能绝对高效
            x ^= y; 
            y ^= x; 
            x ^= y; 
    5. 求绝对值
            int abs( int x ) 
           { 
             int y ; 
             y = x >> 31 ; 
            return (x^y)-y ;        //or: (x+y)^y 
           }
    6.  取模运算,采用位运算实现:
             a % (2^n) 等价于 a & (2^n - 1) 
    7.  乘法运算   采用位运算实现
             a * (2^n) 等价于 a << n
    8.   除法运算转化成位运算
              a / (2^n) 等价于 a>> n 
    9.   求相反数
             (~x+1) 
    10  a % 2 等价于 a & 1 
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325606579&siteId=291194637