C language bit operation

Source: Internet
Author: User
Tags bitwise

Recently re-learning C language, using the book for Brian W.kernignan and Dennis M.ritchie "C programming language." Read the bit operation tonight and write some simple test procedures.

The C language provides 6 bit manipulation operators. These operators can only be used for integer operands, which can only be used with signed or unsigned char, short, int, and long types.

These six bit operator operators are:

& bitwise      and |       bitwise OR ^ bitwise OR      <<    left shift >>    right Shift     -     bitwise negation

Bit operations are commonly used for some switch-type operations. For example:

x = x | set_on

Can be used to indicate that a certain person is 1, if the 3rd bit (from the left, the first bit is 0) is 1, then set_on = 0100

To set a one to 0, the

x = x & Set_off

If the same third position is placed 0, then Set_off = 1011

A more specific usage of bitwise negation is:

x = x & ~077

Place the last 6 bits of x 0. The expression ~077 is not related to the machine word length, which is better than the expression of form 0177700, because the latter assumes that X is a 16-bit value. This portable form does not add extra overhead, because ~077 is a constant expression that can be evaluated at compile-time.

When a right shift is made to the unsigned value of the unsigned type, the left empty portion is filled with 0, and when the signed value of the signed type is shifted to the right, some machines will fill the left empty part with a symbol (i.e. "arithmetic operation"), while others fill the left empty portion with 0 (i.e. " Logical Shift ").

Here are a few examples of using bit operations:

1. Write the function getbits (x,p,n), which returns a field in X that starts at the right number of n bits from the P-bit on the right. This assumes that the rightmost one is the No. 0 position, and both N and p are reasonable positive values.

This is titled The book's Instance program, no longer introduced, the code is as follows:

unsigned getbits (unsigned x,int p,int n) {    1-n)) & ~ (~0 << n);}   

2. Write a function setbits (x,p,n,y) that returns the result value for X after doing the following: Sets the N (binary) bit from the p bit in x to the rightmost n-bit value in Y, and the rest of X remains unchanged

As shown, first the X and ~ (~ (~0 << N) << (P + 1-n)) and, clear 0 x left P-bit to the right n bits, resulting in 1.

Next, Y and ~ (~0 << N) and to clear 0 y in addition to 0 to n-1 bit, in the above results left p + 1-n bit, will be y of the low n shift left, resulting in 2

Finally, result 1 and result 2 or, the result, the code is as follows:

int n, unsigned y) {    return ~ (~ (~1-n)) & X | ((~ (~1- N));}    

3. Write a function invert (x,p,n) that returns the result value for X after doing the following: the N (binary) bit starting from the P bit in X is reversed and the remaining bits of x remain unchanged

This problem is no longer drawing, directly explain the idea can

If a bitwise negation is used, each bit of x is reversed and does not meet the requirements. It is noteworthy that the XOR or ^ operation, 0 with 1 xor result is 1 xor result is 0, visible with 1 xor result is the negation of the bit. Another 0 with 0 XOR result is 0,1 with 0 XOR result or 1, visible with 0 xor result is to keep itself unchanged. This feature can be used to solve the problem. That is, let X be the same as 000011110000 (can be expressed as ~ (~0 << N) << (p + 1-n) value XOR. The code is as follows:

int N) {    return x ^ (~ (~1- N));}   

4. Write a function Rightrot (x,n) that returns the value of the N (binary) bit after the X loop is shifted right (that is, the bit moved from the leftmost position will be moved from the leftmost end)

The basic left-shift and right-shift functions, with the opposite side of the shift, are generally supplemented with 0 or sign bits to achieve the purpose of cyclic displacement. To achieve a cyclic shift, the rightmost n of X is moved first to the left and then the X to the right by n bits, and the two results are either performed or manipulated. The code is as follows:

int N) {    8;    N%=return (~ (~0 << N) & x) << (l-n) | x >> n;}     

5. Finally, a method of finding the number of 1 in the binary of an integer x is presented.

The most direct method of violence is to calculate the binary representation of x directly, and then count the number of 1. But there is a more "smart" way.

To understand a more "smart" idea, you first need to be clear about the meaning of the results of X & (X-1). This action changes the rightmost 1 of the x binary to 0. As for why, readers can think for themselves.

This makes it easy to write the code:

unsigned bitcount (unsigned x) {    0;    1) + + +return i;}     

The above 5 topics cover the basic use of six-bit operations in C language. Learning to use bit operations is not only learning bit operation, I think through this learning method can exercise their own logical thinking, strengthen their sensitivity to logic.

PS: Several of the functions described above have fallen into the pits several times. This pit is the priority of the operator. The precedence of the bitwise operator is lower than the plus (+) minus (-) operator. So x << n + 1 refers to the X left shift n+1 bit, instead of X left N and plus 1. When I first tried to write some of these functions, we used addition, and after thinking, we replaced the addition with or manipulated.

C language 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.