Some simple applications of bit operators

Source: Internet
Author: User
Tags integer division logical operators
In the first section of the overview, it is said that C language is an intermediate language, can be directly operated on computer hardware, which involves the concept of bits.

The concept of a bit
We know that in a computer, a byte occupies 8 bits (some computers now have 16 bits), which means that the number ranges from 0-255 to 00000000-11111111. The bits are 0 and 1 inside.
Char c=100;
In fact, C should be 01100100, which is exactly 64H. Where high is in front, low in the rear.
| |
7th Digit NO. 0 Place

Two-bit logical operators

Symbol description
& Bit Logic and
| Bit logic or
^ Bit logic xor
~ Fetch and fill

The last operator that is removed from the table is the monocular operator, and the other is the binocular operator. These operators can only be used with integer expressions. Bit-logical operators are typically used to set the bits of an integer variable, clear 0, reverse, and detect certain selected bits. It is commonly used by programmers as a switch sign in a program. Lower-level hardware device drivers often require bit operations on the input.

The rule of the & operation is that when two bits are 1 o'clock, the result is 1, otherwise 0;
| The rule of operation is that when two bits are 0 o'clock, the result is 0, otherwise 1;
^ The rule of the operation is when two phases at the same time, the result is 0, otherwise 1;
The rule of the ~ operation is when the result of 1 o'clock is 0, and when it is 0 o'clock, the result is 1.

Set bit: Set a bit to 1, while the other bits remain unchanged, you can use bit logic or operations.
char c;
c=c|0x40;
So no matter how much C was originally, and 01000000 or later, it always makes the 6th bit 1, while the other bits do not change.

Clear bits: Sets a bit to 0, while the other bits remain unchanged. You can use bitwise logic and operations.
c=c&0xbf;
So C and 10111111 and later, always make the 6th bit 0, the other bits remain unchanged.
What if you want to make a bit 1, and the other bits are 0?

Three, the displacement operator
Symbol description
<< left Shift
>> Right Shift

The displacement operator acts on the left side of the variable, and the value of the expression on the right is the number of bits moved, and the result of the operation is the result of the moved variable.
b=a<<2;
The value of a is shifted two bits to the left and assigned a value of B. The value of a itself does not change.

Shift to the left is to fill 0 in the low-level casserole, right shift is to fill 0 on the high. When you move right, you can maintain the sign bit of the result, that is, if the highest bit is 1, the symbol bit is 1 instead of 0.
Programmers often implement integer division with right-shift operators, and left-shift operators to implement integer multiplication. The factor used to achieve multiplication and division must be a power of 2.

Example: Enter an integer to determine that there are several bits in this number 1. For example, enter 67 and the output should be 3. Since the corresponding binary number of 67 is 00000000 01000011 (0043H), there are 3 1 occurrences.
Analysis: To judge is not 1, only need to judge the bit with 1 and later is not 1 can be known. An integer that can be judged 16 times.

Main ()
{
int num,k;
int count=0; /* Number of records 1 */
scanf ("%d", &num);
for (k=0;k<16;k++)
{
if (num&1==1) count++; /* Determine if the lowest bit is 1 */
num>>=1; /* num Move right 1 bit */
}
printf ("%d/n", count);
}

So every time you judge the lowest bit is not 1, after judging, let the front of the right to move one can.
The operation of the bit, the general program is not much, but in the computer hardware operation, it will certainly be involved. For example, we will talk about the serial and sound card operation will need to use some.

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.