Summary of _c language by example of C + + bit operation

Source: Internet
Author: User
Tags bitwise

This paper describes in detail the bit operation of C/s + +, which is a very important concept in the program design. The example of this article has a good review and reference value for the C + + beginners. The specific analysis is as follows:

C + + bitwise operations have the following methods:

One, bitwise operator Operators (Note: The following operators do not change the value of the original variable, just get the result of the operation is a new value)

Bitwise COUNTER: ~

Bit and:&

Bit or: |

Bitwise XOR OR: ^

Left shift operator:<<

Shifts the bits of an operation object all left several bits (bits on the left, the right 0).
If the left-move high does not contain 1, then move each left one bit, which is the equivalent of multiplying by 2.
Right shift operator:>>

The bits of one number is shifted all the right several digits, the positive left is 0, the negative is 1, and the right is discarded.
The operand is shifted one bit to the right, equal to the number divided by 2.
Unsigned Right shift operator:>>>

The >>> operator shifts the individual bits of the expression1 to the right expression2 the specified number of digits. The left vacated bits are padded with zero after the right shift. Remove the right side of the bit is discarded.

Second, 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 struct that provides a label for each field and determines the width of the label field.

The above uses the unsigned int as the basic layout unit of the bit-field structure, so even if a struct has only one-bit member field, the structure is the same size as a unsigned int, with sizeof looking 8, machine sizeof (unsigned int) is 4, if the last one is changed to d:20, then the sizeof of the machine is just 4.

In addition, hexadecimal front plus 0x, octal plus 0

Instance: Writes a function that returns the inverse of the given number to the number of 1,1, that is, 0 variable to 0

Train of thought: The whole number of the entire bit of the reverse, not appropriate. With a different or ^, the given value and a number of the following are all 1 of the remaining 0 ^.

The key to this example is how to get the number of the back that's all 1.

#include <iostream>
using namespace std;
int invert_end (int num,int bits)  //First represents the incoming value, and the second is to let the number of digits of the number reverse the
{
  int mask=0;  At this time all bits are 0
  int temp=1;  At this time its last one was 1 while
  (bits>0)
  {
    mask=mask|temp;  Or, only 1 bits at a time to 1
    temp=temp<<1;  Its only one is 1 bits to the left
    bits--;
  return num^mask;  The latter few of the mask are already 1, XOR or can
}

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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.