Talk C Chestnut Bar (19th: C-language instance-bit operation)

Source: Internet
Author: User


Ladies and gentlemen, crossing, the last time we were talking about the output hexadecimal example, this time we say the example is: bit operation. Gossip

Hugh, the words return to the positive turn. Let's talk C chestnuts together!


Crossing, there are five types of operations, with, or, non-, XOR, or displacement. Let's move on to each of these actions. For illustrative purposes, we

First, to do a uniform explanation, bit operation of the object is bit. If there are three bits: A, B, and C, we all know that the bit value is only 0.

Or 12, so the values of a, B, and C are 0 or 1. We treat A and B as the object of the bitwise operation, and C as the result of the A and B operations.


and Operation: the operator is &. Example: C=a & B. Only the values of A and B are 1 o'clock, and the value of C is 1. In other cases, the value of C is 0.

or action: operator is |. Example: C=a | B. As long as there is a value of 1 o'clock in A and B, the value of C is 1. In other cases, the value of C is 0.

non-operation: the operator is ~. Example: C=~a. If the value of a is 1 o'clock, the value of C is 0. Conversely, the value of C is 1.

XOR operation: the operator is ^. Example: c=a ^ B. The value of C is only 1 if the values of a and B are not the same. In other cases, the value of C is 0.


shift operation: divided into right shift and left shift two kinds. The operators are >> and <<. If you shift to the left, the vacant position on the right will be 0. Right

There are two different cases of displacement: arithmetic shift and logical shift. When using arithmetic shift, the left vacant bit will remove the highest position.

, the other vacancies are 0. When using logical shift, the left vacant bit is filled to 0. In general, the compiler uses arithmetic shifts,

In fact, for unsigned numbers, both the arithmetic shift and the logical shift, the results of their operations are the same. For signed numbers, it is necessary to

Not the same, because the highest bits of the signed number are used to denote symbols, especially the negative numbers in the signed number, the tallest of them is 1.

Note the Shift object when using the right shift operation. In addition, the left-hand operation is equivalent to multiplying by 2 and the right shift operation is equivalent to dividing by 2. Of course

The number of bits to move cannot exceed the maximum number of bits owned by the variable. Because of the high efficiency of the shift operation, it can replace the multiplication method.


Crossing, we have said so many operations, what exactly is the use? Crossing, through the bit operation, you can take out a bit of a string

It is also possible to place 0 and 1 on some positions of a bit string. A common practice is to define a mask first, using a mask to make the original bit string

Operations, such as removing the 4 and 5-bit values from A, c= (a&00011000) >>3.


Crossing, the manipulation of bits is easily confused with or manipulated in logical operations. I am here to give you a description:

In terms of appearance, they are different: use & with Operation && with operations. or operation using |, and operation using | |, and OR manipulating

is significantly different from the operator of the OR operation.

from the heart, they are also different: with or operation is to manipulate the bits in the variable storage unit, the result of the operation is the new variable.

An operation is a true or false judgment of a variable or expression, and its result is either real or false. People can be "outside" and "inner" to the district

To divide or manipulate and to work with or operate.


You crossing, we're going to talk about the bit operation here. I want to know what the following example, and listen to tell.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talk C Chestnut Bar (19th: C-language instance-bit operation)

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.