Simple application-bit computing to learn, binary conversion and bit operations

What is bit computing?

In understanding what is 位运算before, let us first understand what is ? Refers to the minimum unit of the computer stores information in the binary number system, is represented by a 0 or 1. When learning a programming language data types, always told us that intstorage needs 4 字节, in the range -2 147 483 648 ~ 2 147 483 647. In fact, 取值范围it is through calculated, due 1 字节 = 8 位, so intin 1a binary representation 0000 0000 0000 0000 0000 0000 0000 0001. So 位运算that is directly on the integer bits in the memory to operate.

For convenience, the following study to demonstrate the two-byte int

Decimal to binary

Positive integer to binary

Positive integer into a binary commonly used 除二倒取余method. The following shows the binary seeking 10, 13, 42 by this method.

Decimal to binary
As shown, so that an integer constant except two, the result of the remainder written on the right side, when the quotient is zero, stop. 10 so as binary: 0000 0000 0000 1010 binary 13 is 0000 0000 0000 1101 , binary 42 is 0000 0000 0010 1010 . In addition to using 除二倒取余 the method, the bit may correspond to decimal table lists, during binary conversion, just need to be split into a decimal number corresponding to the table and can be decimal. Such as:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1

Such as: binary 10: 10 = 8 + 2: the binary 101042: 42 = 32 + 8 + 2: the binary 10101013: 13 = 8 + 4 + 1: 1101

Negative integers to binary

When seeking negative binary integers, you need to first convert a positive integer negative integer corresponding to binary, then binary negated, then the result plus one. Such as seeking -10binary:

-10 的二进制
先求 10 的二进制
0000 0000 0000 1010
取反:
1111 1111 1111 010111111 1111 1111 0110
复制代码

So -10binary is 1111 1111 1111 0110. Note: The highest level is in binary 符号位, 0 is a positive number, expressed as a negative number, so intthe maximum binary integer is 0111 1111 1111 1111 1111 1111 1111 1111in the range of:- 2 ^{31} \longrightarrow 2 ^ {31} - 1

Bit computing

Bitwise can operate on individual bits of the integer value. Bitwise operators these include the following:

&: 按位与Operator

When two integer values 对应位are on 1, the bit is returned 1, otherwise 0(the same 1as 1). Such as:

10 & 13
10 : 0000 0000 0000 1010
13 : 0000 0000 0000 1101
------------------------------------------
结果:0000 0000 0000 1000
复制代码

|: 按位或Operator

When two integer values 对应位are on 0, the bit is returned 0, otherwise 1(the same 0as 0). Such as:

42 | 13
42 : 0000 0000 0010 1010
13 : 0000 0000 0000 1101
------------------------------------------
结果:0000 0000 0010 1111
复制代码

^: 按位异或Operator

When two integer 对应位numbers are not on the same, this bit returns 1, otherwise return 0(return different 1). Such as:

42 ^ 13
42 : 0000 0000 0010 1010
13 : 0000 0000 0000 1101
------------------------------------------
结果:0000 0000 0010 0111
复制代码

~: 按位取反Operator

The integer value of 对应位the number on negated, 1 to 0, 0 to 1.

42 : 0000 0000 0010 1010
------------------------------------------
~42: 1111 1111 1101 0101
复制代码

<<: 左移Operator

X << yThe integer Xbinary digit, move to the left yposition. Up at the end 0. * Corresponds to the X- 2 ^ and.

13 << 2
13 : 0000 0000 0000 1101
----------------------------------------
<<2: 0000 0000 0011 0100
13 << 2 的结果为:52 -----> 13 * (2^2) = 52
复制代码

>>: 右移Operator

X >> yThe integer Xbinary digit, move to the right yplace. Up at the beginning of the sign bit. Corresponds to X / 2 ^ androunded.

13 >> 2
13 : 0000 0000 0000 1101
----------------------------------------
>>2: 0000 0000 0000 0011
13 >> 2 的结果为:3 -----> 13 / (2^2) = 3 
复制代码
-10 >> 2
-10 : 1111 1111 1111 0110
----------------------------------------
>>2 : 1111 1111 1111 1101
-10 >> 2 的结果为:-3 -----> -10 / (2^2) = -3 
复制代码

Simple application

Judgment parity

Since the end of an odd number is always binary 1, so we can x & 1determine whether the end has 1to decide whether it is an odd number.

// 通过: a&1 的方式取二进制的最后一位。
#include<stdio.h>
int main(){
	int a = -3;
	if(a & 1){
		printf("奇数:%d\n",a);
	}else{
		printf("偶数:%d\n",a);
	}
	return 0;
} 
复制代码

exchange

In exchange, ^the calculation result can be understood as operator 记录两个数的不同. Like I told you have two different colored balls (red, blue) in a box. When the hands come up with a basketball in it, which the rest of the ball without looking, color is definitely red.

#include<stdio.h>
int main(){
	int a = 3;
	int b = 2;
	printf("a = %d, b = %d\n",a,b);
	a = a^b;//告诉 a 和 b 的不同的地方,赋值给 a。
	b = a^b;// b 通过不同记录 a,就可以找到原数a,赋值给 b
	a = a^b;// 此时b的值是原数a,a 通过不同记录 a,就可以找到原数b,赋值给 a
	printf("a = %d, b = %d\n",a,b);
	return 0;
} 
复制代码

Averaging two numbers

By (x & y) + ((x ^ y) >> 1)way of averaging two numbers. It is my understanding: first by (x & y)taking the common portions of the two numbers, then ( x ^ y)the result of adding the remainder of >> 1dividing 2. A decimal number, for example: (12 + 8) / 2; 8 + 4 = 12, 8 + 8 = 0; 8 is so common part, the remaining part is (4 + 0) / 2 = 2, mean of 8 + 2 = 10;

#include<stdio.h>
int avarge(int x, int y){
	return (x & y) + ((x ^ y) >> 1);
}
int main(){
	int a = 6;
	int b = 2;
	printf("a = %d, b = %d,平均数:%d\n",a,b,avarge(a,b));
	return 0;
} 
复制代码

Determining whether a number is a power of two

In binary, 2 的幂次integer containing only one 1, such as 2^2: 0100, 2^3: 1000. So when we get rid of the last bit 1, the result becomes 0a. By x & (x - 1)removing the last bit 1.

#include<stdio.h>
int main(){
	int a = 7;
	if (a & (a-1)){
		printf("a = %d 不是 2 的幂次\n",a);
	} else{
		printf("a = %d 是 2 的幂次\n",a);
	}
	return 0;
} 
复制代码

to sum up

Chapter, we learned how to convert decimal to binary, bit arithmetic operators, simple application-bit computing.

Public number: HarLearn

Public number: HarLearn

Guess you like

Origin juejin.im/post/5d7b13426fb9a06aec266c22