Bit operations: bitwise and, bitwise or, bitwise exclusive or, bitwise left shift, bitwise right shift

Table of contents

1. Supplementary basic knowledge

    (1) Bit operation

    (2) Detailed operation of binary

2. Bit operations

    (1) Bitwise AND (&)

    (2) Bitwise or (|)

    (3) Bitwise XOR (^)

      (4) Bitwise left shift (<<)

    (5) Bitwise right shift (>>)

3. Example of bit operation

    Title description:

    answer:

Fourth, mutual encouragement


1. Supplementary basic knowledge

    (1) Bit operation

    Bitwise operators are faster than general arithmetic operators, and can achieve some functions that cannot be achieved by arithmetic operations (examples will be given later in the article). If you need to achieve high development efficiency when completing the code, bit operations are essential. Bit operations are used to operate on binary bits, including: bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise inversion (~), bitwise left shift (<<) , bitwise right shift (>>).

    (2) Detailed operation of binary

    When performing bit operations, a large number of binary source codes, inverse codes, and complement codes will be used, so friends in need can read my last article! ! !

    Link to the article:   http://t.csdn.cn/dasNJ

2. Bit operations

    (1) Bitwise AND (&)

    1. Range of application: must be carried out within the range of integers

    2. Operation method: convert the integer from decimal to binary number, compare up and down, if there is zero, then it is zero, and if both are 1, then it is 1.

    3. Computational code: two's complement

    4. Output method: The integer expression calculation uses the complement code in the memory, and what is printed and viewed is the source code.

    Example: put 3 & (-5)

// int 为整型4个字节有32个bit位
int a = 3 ;  // 00000000 00000000 00000000 000000011   正数,源码=反码=补码
int b = -5 ; // 10000000 00000000 00000000 000000101   -5的源码
             // 11111111 11111111 11111111 111111010   -5的反码
             // 11111111 11111111 11111111 111111011   -5的补码

int c = a & b ;  //按位与:上下比较,有零则零,两个都为 1 才是 1

             // 00000000 00000000 00000000 00000011   3的补码
             // 11111111 11111111 11111111 11111011   -5的补码

     最终结果// 00000000 00000000 00000000 00000011   ----(3)整数,源码=反码=补码

    (2) Bitwise or (|)

    1. Range of application: must be carried out within the range of integers

    2. Calculation method: if there is 1 in the upper and lower comparison, it is 1, and if both are 0, it is 0

    3. Computational code: two's complement

    4. Output method: The integer expression calculation uses the complement code in the memory, and what is printed and viewed is the source code.

    Example: 3 | (-5)

// int 为整型有4个字节,32个bit位
int a = 3 ;  // 00000000 00000000 00000000 00000011   正数,源码=反码=补码
int b = -5 ; // 10000000 00000000 00000000 00000101   -5的源码
             // 11111111 11111111 11111111 11111010   -5的反码
             // 11111111 11111111 11111111 11111011   -5的补码

int c = a | b ;    //按位或:上下比较,有 1 则 1,两个都是 0 才是 0

              // 11111111 11111111 11111111 11111011   -5的补码
              // 00000000 00000000 00000000 00000011   3的补码

      最终结果// 11111111 11111111 11111111 11111011  为负数,是补码
             // 11111111 11111111 11111111 11111010   是反码,补码-1
             // 10000000 00000000 00000000 00000101   是源码,各个位按位取反-----(-5)

    (3) Bitwise XOR (^)

    1. Range of application: must be carried out within the range of integers

    2. Calculation method: compare up and down, the same is 0, the difference is 1 

    3. Computational code: two's complement

    4. Output method: The integer expression calculation uses the complement code in the memory, and what is printed and viewed is the source code.

    Example: 3 ^ (-5)

// int 为整型有4个字节32个bit位
int a = 3 ;  // 00000000 00000000 00000000 00000011  正数,源码=反码=补码
int b = -5 ; // 10000000 00000000 00000000 00000101  负数,(-5)的源码
             // 11111111 11111111 11111111 11111010  -5的反码
             // 11111111 11111111 11111111 11111011  -5的补码

int c = a ^ b ; //按位异或:上下比较,相同为0,相异为1


// 11111111 11111111 11111111 11111011  -5的补码
// 00000000 00000000 00000000 00000011  3的补码


//异或 :11111111 11111111 11111111 11111000  负数,补码
      // 11111111 11111111 11111111 11110111  反码
      // 10000000 00000000 00000000 00001000  源码-----(-8)

      (4) Bitwise left shift (<<)

    1. Range of application: must be carried out within the range of integers

    2. Operation method: shift the binary number one bit to the left, discard the overflow on the left, and fill in zeros on the right

    3. Encoding operation: Complement

    4. Output method: The integer expression calculation uses the complement code in the memory, and what is printed and viewed is the source code.

    Example: shift a = 4 to the left by one bit

// int 为整型,有4个字节,32个bit位
int a = 4 ;       // 00000000 00000000 00000000 00000100,正数,源码=反码=补码
int b = a << 1 ;  // 把a左移一位,左边丢弃,右边补零
                  // 00000000 00000000 00000000 00001000 ----(8)

    (5) Bitwise right shift (>>)

    1. Range of application: must be carried out within the range of integers

    2. Operation method: move the binary number one bit to the right, discard the right side, and fill in the original sign bit on the left side

    3. Encoding operation: Complement

    4. Output method: The integer expression calculation uses the complement code in the memory, and what is printed and viewed is the source code.

int a = -4 ; // 10000000 00000000 00000000 00000100 负数,源码
             // 11111111 11111111 11111111 11111011  -4的反码
             // 11111111 11111111 11111111 11111100  -4的补码
int b = >> 1 ; // 把b向右移动一位,右边丢弃,左边补原符号位
               // 11111111 11111111 11111111 11111110  负数,补码
               // 11111111 11111111 11111111 11111101  反码
               // 10000000 00000000 00000000 00000010  源码----(-2)

    At this point we can find an integer, shifting to the left has the effect of multiplying by 2, and shifting to the right has the effect of dividing by two.

3. Example of bit operation

    Title description:

    Do not create a third variable, exchange the values ​​​​of variables a and b

    answer:

    (Method 1): simple digital operations, but cannot meet all needs

#include <stdio.h>
int main()
{
	int a = 5;
	int b = 3;
	a = a + b;
	b = a - b;
	a = a - b;
	printf("a=%d b=%d\n", a, b);
	return 0;
}

    If the sum of the two numbers a and b just exceeds the value range of int, the first method is not feasible.

    (Method 2): bit operation, simple and efficient

//找规律
//3^3=0
//5^5=0
//3^5^5=3
//3^5^3=5
//3^5=6
// 异或支持交换律

#include <stdio.h>
int main()
{
	int a = 8;
	int b = 9;
	int z;
	z = a ^ b;  //设置密码z
	b = z ^ b;  //b=a^b^b=a
	a = z ^ a; //a=a^b^a=b
	printf("a=%d b=%d\n", a, b);
	return 0;
}

Fourth, mutual encouragement

    This article is my understanding of bit operations. If you have any questions, you can tell them in the comment area. Let's make progress together, let's work together! ! !

 

Guess you like

Origin blog.csdn.net/weixin_45031801/article/details/126807215