Bitwise operator in C language

Posted by Ash3r on Tue, 08 Mar 2022 14:14:21 +0100

1, Bitwise logical operator

1. Binary inversion or bitwise inversion:~

Unary operator ~ changes 1 to 0 and 0 to 1. example:

~(10011010) 		// expression
01100101			// result
newval = ~val;		// The value of Val will not change, and val will be inversely assigned to newval
2. Bitwise AND:&

Binary operator &, which compares an operand bit by bit. Generate a new value. For each bit, the result is 1 only when the corresponding bit in the next operand is 1

3. Bitwise OR:|

Binary operator |, which compares an operand bit by bit., Generate a new value. For each bit, only the corresponding bit in the next operand is 1, and the result is 1

4. Bitwise exclusive or:^

Binary operator ^, which compares two operands bit by bit., Generate a new value. For each bit, only the corresponding bit in the next operand is 1, and the result is 1

2, Usage: Mask

Bitwise and operations are commonly used in masks. The so-called mask refers to some bit combinations set to on (1) or off (0).
For example, suppose the definition symbol constant mask is 2 (that is, the binary form is 00000010), only bit 1 is 1, and other bits are 0. The following statement

flags = flags | mask

Set all bits except bit 1 in the flags to 0, because the bitwise and operator (&) gets 0 when any bit is combined with 0. The value of position 1 remains unchanged (if position 1 is 1, then 1 & 1 will get 1; if position 1 is 0, then 0 & 1 will also get 0). This process is called "use mask", because the 0 in the mask sings the local voice bit in flags.

3, Usage: open bit

Sometimes it is necessary to open a special position in a value, and colleagues keep other bits unchanged.
For example, suppose the flags are 00001111 and the mask is 10110110. The following expression

flag | mask

That is
(00001111)| (10110110)
The result is
10111111.

4, Displacement operator

1. Shift left

The shift left operator (< <) shifts the value of each bit of its left operand to the left by the number of bits specified by its right operand. The left operand moves out of the left end bit, and the value of the end bit is lost. Fill the empty position with 0.
For example, in the following example, each bit moves a position to the left

(10001010) << 2   	// expression
(00101000)			// Result value

This operation produces a new bit value, but does not change its operand. For example, if stonk is 1, then stonk < < 2 micro, but stonk itself remains unchanged.

2. Shift right

The shift right operator (> >) shifts the value of each bit of its left operand to the right by the number of bits specified by its right operand. The value of the left operand out of the right end bit is lost. For unsigned types, fill the empty position with 0; For signed types, the result depends on the machine.

(10001010) >> 2		// Expressions, signed values
(00100010) 			// Results of some systems
(10001010) >> 2		// Expressions, signed values
(11100010) 			// Results of some systems

The following are unsigned:

(10001010) >> 2		// Expressions, unsigned values
(00100010) 			// Results of some systems

3. Usage: shift operator
The shift operator provides fast and effective division and multiplication for the power of 2:
Number < < 2 number multiplied by 2 to the nth power
Number > > 2 if number is nonnegative, divide number by the n-power of 2

5, Code

#include <stdio.h>
#include <limits. h> 	//  Provide char_ Definition of bit, CHAR_BIT represents the number of bits per byte

char * itobs(int,  char *);
void show_bstr(const char *);


int main()
{
	char bin_str[CHAR_BIT * sizeof(int) + 1];
	int number = 0;
	
	puts("Enter integers and view them in binary form");
	puts("Non digital input termination procedure");
	while(scanf("%d", &number) == 1)
	{
		itobs(number, bin_str);
		printf("%d is", number);
		show_bstr(bin_str);
		putchar('\n');
	}
	
	puts("success!!!");

	return 0;
}


char * itobs(int n, char * ps)
{
	int i = 0;
	const static int size = CHAR_BIT * sizeof(int);
	for(i = size - 1; i >= 0; i--, n >> 1)
	{
		ps[i] = (01 & n) + '0';
	}
	ps[size] = '\0';
	
	return ps;
}

/* 4 A set of bits displays binary strings */
void show_bstr(const char *str)
{
	int i = 0;
	
	while(str[i])		/* Is not an empty character */
	{
		putchar(str[i]);
		if(++i % 4 ==0 && str[i])
		{
			putchar(' ');
		}
	}
}

Topics: C