Operator explanation Part 1

Posted by Andrew R on Wed, 09 Mar 2022 13:04:16 +0100

Shift operators

Shift right

The right shift is divided into:

Arithmetic shift: discard the right and fill the original symbol bit on the left

Shift left: 0, fill right: 0

Positive shift method

A binary number contains the inverse complement of the original code

When shifting, the complement is moved

Positive number

For example:

int a=16;

Original code, inverse code and complement code are

00000000000000000000000000010000

If a is shifted one bit to the right

int b = a>>1;

The right side of the complement is discarded and the sign bit is added to the left.

The complement becomes: 000000000000000000000000000

Because the original and inverse complement of positive numbers are the same, they are converted to decimal.

a becomes 8.

Negative shift method

For example:

int a=-1;
int b=a>>1;

The binary source code whose first bit is sign bit a is

10000000000000000000000000000001

Inverse code: the sign bit remains unchanged, and other bits are reversed

111111111111111111111111111111111110

Complement: inverse code + 1

111111111111111111111111111111111111

After moving right

Lost the 1 on the right and added a 1 on the left. There is no change

So after moving right, b is still - 1.

Shift left

There is only one way

Discard on the left and fill 0 on the right

Bitwise operators

Bitwise operators are:

&Bitwise AND;

|Bitwise OR;

^Bitwise XOR;

Bitwise AND

int c = a&b;

Compare the complement. If the complement binary of a is the same as that of b, the binary bit of c is 1;

For example, complement a: 00000000000000000101

Complement b: 0000000000000000000000

Complement c: 0000000000000000000000

Bitwise OR

int c = a|b;

Compare the complement. If the complement binary of a is the same as that of b, one bit is 1, then the binary bit of c is 1;

For example, complement a: 0000000000000111

Complement b: 0000000000000000000000

Complement c: 000000000000000000111

Bitwise XOR

   

int c = a^b;

Compare the complement. In the complement of c, if the complement binary of a is the same as that of b, the bit is 0 and the different bit is 1;

For example, complement a: 0000000000000111

Complement b: 0000000000000000000000

Complement c: 000000000000000000000000000000 11

Practical interview questions

Exchange the values of integer a and integer b without establishing temporary variables

Writing 1: simple writing

int main()
{
	int a = 3;
	int b = 5;
	a = a + b;
	b = a - b;
	a = a - b;
	printf("a = %d\nb = %d ", a, b);
	return 0;
}

Notation 2: XOR notation

Use the value of a ^ b as the password.

int main()
{
	int a = 3;
	int b = 5;
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("a = %d\nb = %d ", a, b);
	return 0;
}

HKCEE, but not practical.

Exercise: find the number of 1 in the binary number of an integer stored in memory

The first way to write: negative numbers are not available

#include<stdio.h>
int main()
{
	int a = 0;
	scanf("%d", &a);
	int count = 0;
	while (a)
	{
		if (a % 2 == 1)
			count++;
		a = a / 2;
	}
	printf("%d", count);
	return 0; 


}

Assignment operator

Compound assignor

int a = 0;
a+=1 == a=a+1;
a&=1
a*=2
a*=2
a%=2

Like this

unary operator

There is only one operand

 ! Operator

True becomes false, false becomes true

int a = 0;
a=!a;
a==1;
int a = 10;
a=!a;
a==0;

Application scenario

int a = 0;
if(!a)//If a is false, print hehe
{
printf("hehe\n");
}

Fetch address operator&

int a = 10;
int* p = &a;//Store the address of a with a pointer
*P ; //Dereference operation, the result is the value of a

Type length of operand sizeof

int main()
{
	char c = '0';
	int arr[10] = { 0 };
	int a = 0;
	char* p = &c;
	printf("%d", sizeof(a));//4
	printf("%d", sizeof(int));

	printf("%d", sizeof(c));//1
	printf("%d", sizeof(char));

	printf("%d", sizeof(p));//4
	printf("%d", sizeof(char*));

	printf("%d", sizeof(arr));//40
	printf("%d", sizeof(int[10]));
	//For arrays, removing the name leaves the type
}

For arrays, the only thing left after removing the name is the type

Classic questions

int main()
{
	short s = 0;
	int a = 0;
	printf("%d\n", sizeof(s = a + 5));
	printf("%d\n", s);


}

Guests are welcome. sizeof is short integer

The value of s has not changed.

So it's printed out as 2 and 0

Topics: C