[CC ++ language entry]-bit operations

Source: Internet
Author: User

Looking back at the previous sections, the main part of the C language has basically been introduced. The reason why I didn't introduce the related features of C ++ is that C and C ++ have commonalities in these aspects in the previous articles, so before object-oriented. We will introduce these commonalities first. That is to say, all articles can be used in CC ++ before the introduction of object-oriented. From this point of view, it is still very useful for beginners who are struggling on the C ++ front.

This article continues along this route. So far, this article does not rush to introduce the object-oriented features of c ++. In the previous article, we can say that the content is basically described. Although this article is not a big concept, it is absolutely indispensable in actual projects. In this article, we will start our bit operation journey.

First, what is bitwise operations used for? It seems that I have not used bitwise Operations yet? I believe many beginners will have such questions. This article will introduce the powerful usage and infinite charm of bit operations.

Bitwise operations are closely related to binary operations. We believe that the concept of binary is familiar to everyone. Our bitwise operations are performed on these 0 or 1 operations. You don't know about binary. For example:

The 8-bit binary value of 7 is: 0000 0111.

7 32-bit binary: 0000 0000 0000 0000 0000 0000 0000 0111

I will not talk about the conversion between binary and decimal. Why does the above three 1 represent 7? If you don't know, read the book.

We have mentioned 8-bit and 32-bit values above. We know that a byte represents 8-bit values, so the binary bit indicates this bit. If the int value is 32 bits, the binary value of the complete number 0 is 32 bits. In this way, it is better to understand bit operations.

Let's take a look at the bitwise operators we often use: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR or ),~ (Bitwise inversion),> (shifted right by bit), <(shifted left by bit ).


& (Bitwise AND): In terms of concept, the bitwise AND operation is performed on the binary system based on each bit (0 or 1. So I don't need to talk about the meaning of the operation, that is, both of them are 1 and the result is true. If one of them is 0, the result is false. There cannot be a number other than 0 or 1, which is binary. First, let's look at an example of an 8-bit binary system:

7 & 8 = 0000 0 111 & 0000 1000 = 0000 0000 = 0

7 & 3 = 0000 0111 & 0000 0011 = 0000 0011 = 3

It's easy. Needless to say, the operations are 0 and 1.


| (Bitwise OR): In terms of concept, the bitwise OR operation is performed on the binary system based on each bit (0 or 1. So I don't need to talk about the meaning of the or operation, that is, the result of both 0 is false. All other cases are true.

 

7 | 8 = 0000 0 111 | 0000 1000 = 0000 1111 = 15

7 | 3 = 0000 0111 | 0000 0011 = 0000 0111 = 7


^ (Bitwise exclusive or): In terms of concept, an exclusive or operation is performed on the binary system based on each bit (0 or 1. An exclusive or operation simply means that the same operation is false, and the difference is true.

7 ^ 3 = 0000 0111 ^ 0000 0011 = 0000 0100 = 4


~ (Bitwise inversion): In terms of concept, bitwise inversion is performed based on each bit (0 or 1) in binary. In simple words, the inverse operation is changed from 0 to 1 and from 1 to 0.

~ 7 = ~ 0000 0111 = 1111 1 000 = 0xf8 = 248 (unsigned)


> (Shift right by bit): In terms of concept, the right shift operation is performed on the binary system based on each bit (0 or 1. In simple words, the right shift operation moves the binary bit to the right.

7> 2 = 0000 0111> 2 = 0000 0001 = 1 // here, two bits are moved to the right, and the two bits are erased.

Here, the two shifts to the right are equal to the 2nd power except for 2. in integer division, 7/4 = 1 is regarded as the decimal part.


<(Shift left by bit): This is not to be mentioned. It is opposite to the previous one.

 


Now, we have a basic concept. Then we will go to the actual application.


We all know the color, for example, If you provoke me again. I will show you the color. The color here is RGB. Here we will talk about the 24-bit color. That is, R (red), G (green), and B (blue) in RGB occupy 8 places respectively. Now some friends are wondering, 24 digits? What should I do if there are no 24-Bit Data Types in the previous basic data types?

So we use bitwise operations. A 32-bit unsigned integer with a height of 8 and zero. The 24-bit low color is used to indicate the color, so some friends may think about it again. How does one express a 24-bit low? We all know that the color is usually 0 to 0 for each component ~ Between 255, how do I store the three colors in 24 bits?

Typedef unsigned char BYTE;

Typedef unsigned int UINT;


BYTE r = 255;

BYTE g = 255;

Bytes B = 255;


We set the three components to 255, and The purpose here is to represent white.


UINT color = (r <16) | (g <8) | B;

Then the color becomes white.

The principle here is simple:

0000 0000 1111 1111 1111 1111 1111

I have marked the font color for all the color components here. If the red part is shifted to the left by 16 digits, the specific process is as follows:

R <16

0000 0000 1111 1111 0000 0000 0000

G <8

0000 0000 0000 0000 1111 1111 0000

B

0000 0000 0000 0000 0000 0000 1111

Then we can see that the three binary values are our target colors after the bitwise OR operation. In hexadecimal notation, the values are 0x00ffff. 0xff is 255.

The 32-bit color only has one more component than the 24-bit color and can be used for transparency. That is, the maximum 8 bits we have not used above. 32-bit can also put the 8-bit high component in the 8-bit low, RGB in the 24-bit high. For example:

1111 1111 1111 1111 1111 1111 1111

Now that we know the color, what should we do if we want to obtain a component? Simple:

BYTE r = (color> 16) & 0xff;


BYTE g = (color> 8) & 0xff;


The above three statements are equivalent to inverse operations. So what is the principle of bitwise and the previous 0xff? Let's look at the g component:

Color> 8

0000 0000 0000 0000 1111 1111 1111

0xff

0000 0000 0000 0000 0000 0000 1111

Do they remove the red component?

0000 0000 0000 0000 0000 0000 1111


There are only eight green ones left. Here I just raised 255, so some friends may say that I am directly:

BYTE g = (color> 16) & 0xff; this is equal to 255. Here is a special example. When r g B is not equal here, it cannot be used. Here is a general usage and we cannot be specialized.


Let's take a look at the 16-digit RGB565. the literal meaning is that r and g occupy 5 places, and B occupies 6 places. A total of 16 digits. If it is 16 bits, we do not need a UINT. We only need:

Typedef unsigned short UINT16;

 

BYTE r = 255;

BYTE g = 255;

Bytes B = 255;


UINT16 color16 = (r & 0xf8) <8) | (g & 0xfc) <3) | (B & 0xf8)> 3 );

 


God, some friends may be dizzy when they see this string. In fact, we have encountered this problem, if you are not familiar with the hexadecimal number, you can use the calculator that comes with WINDOWS. Let's explain it step by step.

Because it is the color of the "565" mode, r needs to discard the low 3 bits and only needs the high 5 bits. G needs to discard the low 2 bits, As long as 6 bits, B and r are the same, and also discard the low 3 bits. The total number is 16 bits. Then we need to save these 16 bits for the three parts respectively. It is also a bitwise OR operation. R only has five digits in height. To reach the maximum five digits of UINT16, You need to shift the value to the left by eight digits.


0000 0000 1111 1000 // It is obvious that you need to move 8 digits to the left

After B is discarded and the lower two bits are removed:

1111 1111 1100 // it is obvious that three digits need to be moved to the left

B component:

1111 1 111 1 11 1 1111 000 // obviously two more 0 s need to move three to the right

You don't need to mention the algorithms that discard low positions. If you are not familiar with it, use a calculator to calculate the desired result. Because of this, the 16-bit color is not 24-bit.


Question 1: why should we abandon the low position instead of the high position? (For example, red can be: r & 0x1f)


We already know that the 24-bit color in turn returns the inverse operation to obtain each component, so:

Question 2: How to obtain each component in the RGB565 color color16.


After learning about the colors above, I believe that everyone has no problem with the & | <> colors. Of course, there are other colors in the combination. This is not to introduce colors. It is to understand bitwise operations.

Bit operations are flexible. Here is just a basic introduction. More practices are needed.

After learning about the operators above, we will introduce the remaining two operators: bitwise inversion and bitwise XOR.

In actual work, there are usually some States that need to be expressed. We want to save some space for these statuses. So we chose a 32-bit unsigned integer to store these statuses. For example:

In the game, some statuses of a player are the BUFF that we often call. For example, continuous addition of blood, continuous addition of blue, continuous addition of physical strength, meridian injury, and points of interest. So we have an enumeration:

Enum EPLAYER_STATE

{

EPST_NONE = 0x00000000, // No status

EPST_ADDHP = 0x00000001, // Add blood

EPST_ADDMP = 0x00000002, // Add blue

EPST_ADDSP = 0x00000004, // physical strength

EPST_JMDAM = 0x00000008, // meridian Injury

EPST_DIANX = 0x00000010, // points

EPST_XUANY = 0x00000020, // dizzy

EPST_ATTCK = 0x00000040, // attacked

//.

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.