当前位置:网站首页>Logical operation bit operation

Logical operation bit operation

2022-06-13 01:21:00 Zhao_| adult

1、 Original code 、 Inverse code 、 Complement code 、 Shift the code

Original code 、 Inverse code 、 Complement code

  • Positive numbers : The original code of a positive number 、 Inverse code 、 The complement is the same
  • negative :
    • Original code : The highest bit represents the sign bit , The negative sign bit is 1, The sign bit of a positive number is 0;
    • Inverse code : The inverse of a negative number is based on its original code , The sign bits remain the same , The rest of the bits are reversed .
    • Complement code : Complement code = Inverse code +1; Sign bits can be carried .
    • Shift the code : On the basis of complement Sign bit inversion . Code shift can only be used to represent integers , Cannot represent decimal
    • Replace the complement code with the original code : It is the same as converting the original code to the complement :–> Inverse code +1;

2、 Binary system 、 octal 、 Decimal system 、 Conversion between hexadecimals

Binary system 、 octal 、 Decimal system 、 Conversion between hexadecimals

3、 Floating point decimal conversion

Floating point decimal conversion

4、 Negative binary conversion

Negative binary conversion

5. Logical operations

What is true ? : All nonzero values are true ;
What is false ?: 0 On behalf of false ;

1. Logic and &&

If and only if two operands are true ( Nonzero ) The result is true when ;

int x=100;
int y=50;
cout<<(x&&y)<<endl;	//1

int x=100;
int y=0;
cout<<(x&&y)<<endl;	//0

2. Logic or ||

As long as one of the two objects is true ( Nonzero ) That's true ;

int x=100;
int y=0;
cout<<(x||y)<<endl;	//1

int x=0;
int y=0;
cout<<(x||y)<<endl;	//0

3. Logic is not !

Reverses the value of the operand and returns ;
** Not 0 Take the opposite ⇒ 0; 0 Take the opposite ⇒ 1; **

int x=100;
cout<<(!x)<<endl;	//0

int x=0;
cout<<(!x)<<endl;	//1

The value here is negative And According to the not Is different , According to the not Need to be converted to Binary system , Then perform bit wise inversion ;

6. An operation

Convert it to Binary system It's going on An operation ;

1. Bit and &

35:  0 0 1 0 0 0 1 1
47:  0 0 1 0 1 1 1 1
&
————————————————————
35:  0 0 1 0 0 0 1 1

 int x=35;
 int y=47;
 cout<<(x&y)<<endl; //35

2. Bit or |

35:  0 0 1 0 0 0 1 1
47:  0 0 1 0 1 1 1 1
|
————————————————————
47:  0 0 1 0 1 1 1 1

 int x=35;
 int y=47;
 cout<<(x|y)<<endl; //47

3. Bit inversion ~

35:  0 0 1 0 0 0 1 1
~
————————————————————
92: 1 1 0 1 1 1 0 0  // The first bit is the sign bit 
 int x=35;
 cout<<(!x)<<endl; // -36

As can be seen from the above 35 The output of the program is the same as The result of bitwise negation is inconsistent ?
Because negative numbers are usually used in calculations Complement code Express : So we should convert the inverse result into Complement code

35:  0 0 1 0 0 0 1 1
~
————————————————————
92: 1 1 0 1 1 1 0 0  // Original code 

		 1 0 1 0 0 0 1 1	// Inverse code  : The sign bits remain the same , The rest of the bits are reversed 
		+1
	————————————————————
	-36: 1 0 1 0 0 1 0 0  // The first bit is the sign bit 

4. Bit exclusive or ^

Take the same 0 , The similarities and differences are 1;

35:  0 0 1 0 0 0 1 1
47:  0 0 1 0 1 1 1 1
^
————————————————————
12:  0 0 0 0 1 1 0 0

int x=35;
int y=47;
cout<<(x^y)<<endl; // 12
原网站

版权声明
本文为[Zhao_| adult]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280553073260.html