位运算_action

计算机系统基础(一):程序的表示、转换与链接-模块四 第2讲 整数除法运算-网易公开课 https://open.163.com/newview/movie/free?pid=WFVPGEQSL&mid=KFVPGF2V4

 

 Bitwise and Bit Shift Operators 位运算 取反  补码 

 

 

 

字节 8位

 

计算机系统基础(一):程序的表示、转换与链接-模块三 第3讲 C语言中的各类运算-网易公开课 https://open.163.com/newview/movie/free?pid=WFVPGEQSL&mid=IFVPGF1U9

 

 

 

 

 原码:符号位0正,1负。

整数x原=

0,x  2^n>x>=0 

2^n-x 0>=x>-2^n

正0负0

小数x原=

x  1>x>=0

1-x 0>=x>-1

x为真值

 

 

&^   bit clear (AND NOT)    integers

https://golang.google.cn/ref/spec#Arithmetic_operators

Go语言的bit clear操作

按位清除

以下摘自The Go Programming Language

The &^ operator is bit clear (AND NOT): in the expression z = x &^ y, each bit of z is 0 if the corresponding bit of y is 1; otherwise it equals the corresponding bit of x.

z = x &^ y运算相当于先把y取反(针对y的每个bit0变成11变成0),然后再和x进行&运算。参考下例:



Go语言的bit clear操作 | 我的站点 https://nanxiao.me/golang-bit-clear-operation/

取出 右侧的0位对应的左侧的位的值




#include <stdio.h>
int br(void)
{
    printf("\n");
    return 0;
}
int main(void)
{
    int bit_a = 5;
    printf("bit_a & 3  = %d", bit_a & 3);
    printf("bit_a | 3  = %d", bit_a | 3);
    printf("bit_a ^ 3  = %d", bit_a ^ 3);
    br();
    // 数字0,8进制
    // 位运算,统一转为2进制。
    printf("0177  = %d,", 0177);
    printf("0177  = %o,", 0177);
    printf("bit_a & 0177  = %d,", bit_a & 0177);
    br();
    printf("bit_a & 0177  = %d", bit_a & 0177);
    printf("bit_a | 0177  = %d", bit_a | 0177);
    printf("bit_a ^ 0177  = %d", bit_a ^ 0177);
    br();
    printf("0x0  = %d,", 0x0);
    printf("0xf  = %d,", 0xf);
    // 0x177 1,0111,0111 :5 375 370
    printf("bit_a & 0x177  = %d,", bit_a & 0x177);
    printf("bit_a | 0x177  = %d,", bit_a | 0x177);
    printf("bit_a ^ 0x177  = %d", bit_a ^ 0x177);
    br();
    // 0177   111,1111  :111,0111  1,0111,1111  1,0000,1000  :119,383,264
    printf("0177 & 0x177  = %d,", 0177 & 0x177);
    printf("0177 | 0x177  = %d,", 0177 | 0x177);
    printf("0177 ^ 0x177  = %d", 0177 ^ 0x177);
    br();
    printf("bit_a << 3  = %d,", bit_a << 3);
    printf("bit_a >> 3  = %d,", bit_a >> 3);
    printf("~bit_a  = %d", ~bit_a);
    br();
    return 0;
}

  

 

bit_a & 3  = 1bit_a | 3  = 7bit_a ^ 3  = 6
0177  = 127,0177  = 177,bit_a & 0177  = 5,
bit_a & 0177  = 5bit_a | 0177  = 127bit_a ^ 0177  = 122
0x0  = 0,0xf  = 15,bit_a & 0x177  = 5,bit_a | 0x177  = 375,bit_a ^ 0x177  = 370
0177 & 0x177  = 119,0177 | 0x177  = 383,0177 ^ 0x177  = 264
bit_a << 3  = 40,bit_a >> 3  = 0,~bit_a  = -6

  

 

 

Bitwise and Bit Shift Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Bitwise and Bit Shift Operators

The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this section are less commonly used. Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist.

The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

The bitwise & operator performs a bitwise AND operation.

The bitwise ^ operator performs a bitwise exclusive OR operation.

The bitwise | operator performs a bitwise inclusive OR operation.

The following program, BitDemo, uses the bitwise AND operator to print the number "2" to standard output.

class BitDemo {
    public static void main(String[] args) {
        int bitmask = 0x000F;
        int val = 0x2222;
        // prints "2"
        System.out.println(val & bitmask);
    }
}


位运算


https://baike.baidu.com/item/取反/12718166?fr=aladdin

运算方法

正数取反

先将初始数值转换成二进制数,再对二进制数的每一位(包括第一位的符号位)进行运算:即将0变为1、将1变为0。得到的是最终结果的补码,要转换为最终结果的原码则需再次取补码,就能得到计算结果。
【例1】对 5 进行取反。
假设为16位。
5转换为二进制数为: 0000 0000 0000 0101得到二进制数
每一位取反: 1111 1111 1111 1010得到最终结果的补码
取补码: 1000 0000 0000 0110得到最终结果的原码
转换为十进制数:-6
则 5 取反为 -6 .
 

负数取反

先将初始数值转换成二进制数,再取得二进制数的补码,之后对补码的每一位(包括第一位的符号位)进行运算:即将0变为1、将1变为0。得到的是最终结果的补码(到达这一步后所得的二进制数为正数,由于正数的原码、反码、补码相同,后面的运算可以忽略,视此步得到的为最终结果的二进制数),要转换为最终结果的原码则需再次取补码,就能得到计算结果。
【例2】对 -5 进行取反。
假设为16位。
-5 转换为二进制数为: 1000 0000 0000 0101得到二进制数
取补码: 1111 1111 1111 1011得到二进制数的补码
每一位取反: 0000 0000 0000 0100 得到最终结果的补码
取补码: 0000 0000 0000 0100得到最终结果的原码
转换为十进制数:4
则 -5 取反为 4 .
 

简便方法

也可以用适合人类运算的计算方法:
如对 a 按位取反,则得到的结果为 -(a+1) .
此条运算方式对正数负数和零都适用。
 
 https://baike.baidu.com/item/补码/6854613?fr=aladdin
 
 
Number Systems http://www2.elo.utfsm.cl/~lsb/elo211/aplicaciones/katz/chapter5/chapter05.doc1.html
 



int bitmask = 0x000F; //15 1111
int val = 0x22; //2*(16^2-1)/15=16*2+2=34 11111+11=100010
System.out.println(val);
System.out.println(bitmask);
System.out.println(~val);
System.out.println(~bitmask);
System.out.println(val & bitmask);// a bitwise AND operation // 10 2
System.out.println(val ^ bitmask);// a bitwise exclusive OR operation // 101101 45
System.out.println(val | bitmask);// a bitwise inclusive OR operation // 101111 47

 

 

Bitwise and Bit Shift Operators 位运算 取反  补码

 

 

异或 XOR ^

满足交换律、结合律
n^n=0
n^0=n

x^y^y=x

 

 

【技巧总结】位运算 -CSDN博客 https://blog.csdn.net/m0_37907797/article/details/103120886

1、一组数,除去唯一的一个数外,其他数重复出现偶数次,查出该数,要求时间复杂度O(n),空间复杂度O(1)实现

x=a1^a2...ai...an

2、不引入第三个数,交换两个数
x=x ^ y
y=x ^ y=x0^y0^y0=x0
x=x ^ y
=(x0^y0)^((x0^y0)^y0)
=y0


 

且运算

x&1=1 等价于x为素数 判断奇偶数

 

移位运算

 

幂运算 求m的n次方,常规做法时间复杂度O(n)

 

异或运算 交换两个数  位运算  空间复杂度O(1)

 

如何优雅地运用位运算实现产品需求? | 梁桂钊的博客 http://blog.720ui.com/2020/bitdesign/

 

leetcode https://blog.csdn.net/holmofy/article/details/79360859

 
Bit Twiddling Hacks http://graphics.stanford.edu/~seander/bithacks.html

 

被3整除

 (n-3) & 0x3 == 0

每个3纳秒
 time.Now().UnixNano() & 0x3 == 0
 

 

posted @ 2018-01-30 12:26  papering  阅读(361)  评论(0编辑  收藏  举报