C语言位运算 Bitwise Operator

Bitwise Operators in C Programming Language

When I first learn C, I found it’s hard to understand the set of bitwise operations, it took me long time to search useful resources while still don’t make sense. However my life changed until I found a famous book wrote by K&RC, The C Programming Language. It is the work of Brian Kernighan and Dennis Ritchie (who created the C language), I Strongly Recomand to Read this book if you want to learn C.


1. Bitwise Operators

C provide six operators for bit manipulation, they are applied to integral operands, char, short, int and long:

Op.Desc.
&bitwise AND
|bitwise inclusive OR
^bitwise exclusive OR
<<left shift
>>right shift
~one’s complement (unary)

2. How to Use ?

  • Bitwise AND & often used to mask off some set of bits:
n = n & 0177;  // set to zero all but lower 7 bits
  • Bitwise inclusive OR | is used to turn bits on:
x = x | SET_ON;  // sets to one in x the bits that are set to one in SET_ON
  • Bitwise exclusive OR operator ^ sets a one in each bit position where its operands have different bits, and zero where they are the same.

As an illustration of some of the bit operators, consider the function getbits(x,p,n) that returns the (right adjusted) n-bit field of x that begins at position p:

// getbits: get n bits from the position p
unsigned getbits(unsigned x, int p, int n) {
    return (x >> (p - n + 1)) & ~(~0 << n);
}

After you finishing read of bitwise section in the book, there are three exercises that is helpful for understanding how to use those bitwise operators. I paste my solutions as follows:

Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

unsigned setbits(unsigned x, int p, int n, unsigned y)
{
    unsigned selectBits = ~(~0 << n) << (p - n + 1);
    unsigned selectElseWhereX = x & ~selectBits;
    unsigned selectY = y & selectBits;
    return selectElseWhereX | selectY;
}

int main(int argc, char const *argv[])
{
    int x = 0xAAFF;
    int y = 0xCCBB;
    // set rightmost 8 bits of x to the rightmost 8 bits of y
    x = setbits(x, 7, 8, y);
    printf("New x = %X", x);  //0xAABB
    return 0;
}

Exercise 2-7. Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.

unsigned invert(unsigned x, int p, int n)
{
    unsigned selectBits = ~(~0 << n) << (p - n + 1);
    unsigned selectElseWhereX = x & ~selectBits;
    unsigned invertBits = ~(x & selectBits) & selectBits;
    return selectElseWhereX | invertBits;
}

int main(int argc, char const *argv[])
{
    int x = 0xAAFFAA;
    // invert 8 bits starting at position 15
    x = invert(x, 15, 8);
    printf("New x = %X", x);  //0xAA00AA
    return 0;
}

Exercise 2-8. Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n positions.

unsigned rightrot(unsigned x, int n)
{
    unsigned selectBits = ~(~0 << n);
    unsigned lowerToUpper = (x & selectBits) << (sizeof(x)*8 - n);
    unsigned upperToLower = (x & ~selectBits) >> n;
    return lowerToUpper | upperToLower;
}

int main(int argc, char const *argv[])
{
    int x = 0xAABBCCDD;
    // right rotate at position 12
    x = rightrot(x, 12);
    printf("New x = %X", x);  //0xCDDAABBC
    return 0;
}

If this artical is useful for you, please give me a thumbs up, thank you!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值