Summary of C and C++ bit operations

  • 2020-04-02 02:41:32
  • OfStack

This article describes in detail the bit operation of C/C++, which is a very important concept in C/C++ programming. This example also has a good review and reference value for C/C++ beginners. Specific analysis is as follows:

C/C++ counterpoint operation has the following methods:

One, bit operation operator (note: the following operators do not change the value of the original variable, but only obtain a new value as the result of the operation.)

Inverted by bit :~

To: &

Or: |

An exclusive or: ^

Left shift operator: < <

Moves all the bits of an operand to the left by several bits (the left bits are discarded, and the right bits are filled with zeros).
If the high position discarded during the left shift does not contain 1, then each left shift is equal to the number times 2.
Right shift operator: > >

Move all the binary bits of a number to the right by several bits, positive left complement 0, negative left complement 1, discard the right side.
Every time the operand moves one bit to the right, that's the same thing as dividing it by 2.
Unsigned right shift operator: > > >

> > > The operator moves bits of expression1 to the right by the number of bits specified by expression2. The bits left empty after moving to the right are filled with zeros. The bits removed from the right are discarded.

Two, bit field

C++ code is as follows:


struct bits
{
  unsigned int a:1;
  unsigned int b:1;
  unsigned int c:10;
  unsigned int d:21;
};

A bit field is declared by a structure that provides a label for each field and determines the width of the label field.

The above one USES unsigned int as the basic layout unit of the bit field structure, so even if a structure has only one member field, the structure is the same size as an unsigned int, with sizeof as 8, machine sizeof(unsigned int) as 4, and if the last one is changed to d:20, the machine's sizeof is exactly 4.

In addition, add 0x to hexadecimal and 0 to octal

Example: write a function that returns an inversion of the next given digit of a given number, i.e. 0 to 1,1 to 0

The idea: is to take the whole number of all the bits of the inverse, not suitable.

The key in this case is how do you get the number that's all 1's


#include <iostream>
using namespace std;
int invert_end(int num,int bits)  //The first is the value passed in, and the second is how many bits are reversed after that value
{
  int mask=0;  //All the bits are zero
  int temp=1;  //In this case, the last digit is 1
  while (bits>0)
  {
    mask=mask|temp;  //Or, I'm just going to put 1 in the 1 position at a time
    temp=temp<<1;  //The only one of them is the bit of 1 shifted to the left
    bits--;
  }
  return num^mask;  //The last few bits of the mask are already 1, xor can be
}

int main(void)
{
  cout<<sizeof(int)<<endl;
  int val;
  cout<<" Enter a positive integer :n";
  cin>>val;
  int res=invert_end(val,3);
  cout<<val<<" "<<res;
  cin.get();
  return 0;
}

Related articles: