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 yx = setbits(x, 7, 8, y);printf("New x = %X", x);  //0xAABBreturn 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 15x = invert(x, 15, 8);printf("New x = %X", x);  //0xAA00AAreturn 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 12x = rightrot(x, 12);printf("New x = %X", x);  //0xCDDAABBCreturn 0;
}

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

C语言位运算 Bitwise Operator相关推荐

  1. (转)C语言位运算详解

    地址:http://www.cnblogs.com/911/archive/2008/05/20/1203477.html C语言位运算详解 作者:911 说明:本文参考了http://www2.ts ...

  2. C语言位运算,醍醐灌顶式教学

    C语言位运算,醍醐灌顶式教学 学习计算机的都了解,位运算其实以一种比较快速的运算,对于大型应用程序,可以节约很多运算时间.但是很多人都不了解,下面我来给大家大致讲解一下C语言中各种位运算符.(小声bb ...

  3. c语言位运算负数的实例_巧妙运用C语言位运算

    原标题:巧妙运用C语言位运算 位运算 位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位串信息的结果. 位运算符有: &(按位与) ...

  4. 在php中将5按位与运算,PHP 5.2和PHP 5.3中对大整数的按位运算(Bitwise operations on big integers in PHP 5.2 and PHP 5.3)...

    PHP 5.2和PHP 5.3中对大整数的按位运算(Bitwise operations on big integers in PHP 5.2 and PHP 5.3) 我将省略有关我是如何做到这一点 ...

  5. c语言位运算(c语言回文数编程)

    C语言的位运算 <>2;/其实这些符号都是差不多的用法;///PUR0 = PUR0 | 0X20;/,就是相当于R0CONH = R0CONH & 0XFC;/left shif ...

  6. c语言位运算负数的实例_一招教你学会C语言中位运算

    程序中的所有数在计算机内存中都是以二进制的形式储存的.位运算说穿了,就是直接对整数在内存中的二进制位进行操作.注意,位运算只针对于整数进行操作. 运算符号 运算规则 1.&与运算:对应两个二进 ...

  7. sizeof是c语言的一种运算符,C语言位运算和sizeof运算符详解

    位运算和sizeof运算符 C语言中提供了一些运算符可以直接操作整数的位,称为位运算,因此位运算中的操作数都必须是整型的.位运算的效率是比较高的,而且位运算运用好的话会达到意想不到的效果.位运算主要有 ...

  8. c语言位运算负数的实例_0基础学习C语言第三章:位运算

    C语言提供了六种位运算符: & 按位与 | 按位或 ^ 按位异或 ~ 取反 << 左移,相当与*2 >> 右移,正数高位补0,负数由计算机决定 循环左移k次 (x< ...

  9. 单片机c语言位运算写法,单片机与嵌入式系统中C语言的位运算小记

    原标题:单片机与嵌入式系统中C语言的位运算小记 编了个PIC的项目,对里的还是蛮有感悟的,特此记录一下. 譬如说,在程序中定义了一个char类型的变量, purge_short_enable_flag ...

最新文章

  1. pythonurllib标准_Python标准库urllib2的一些使用细节总结
  2. Node.js 开发指南笔记
  3. python使用函数的优点-原来 Python 还有这些实用的功能和特点!
  4. MPU6050开发 -- 进阶之I2C/SPI通信协议
  5. Windows 7 硬盘安装方法
  6. Springboot 集成 Swagger
  7. IT 拉呱室 | 论我遇到的最刺激的bug【长期福利站】
  8. LoRa VS NB-IoT,一场物联网时代 C 位争夺战
  9. 13 Django组件- cookie与session
  10. C#无法将顶级控件添加到控件 新的子窗体无法添加到主窗体
  11. 淘宝用户分析(步骤详细,数据分析项目)
  12. pwnable.kr第二遍---mistake
  13. ANSYS公开课圆满落幕
  14. matlab抠图数据,MATLAB抠图
  15. EXCEL POI单元格下拉的两种实现方式
  16. JAVA疫情数据项目(JAVA课程设计)
  17. Markdown Cheat Sheet
  18. Matlab/Simulink中的S函数模块嵌入人工智能、神经网络算法设计仿真案例详解(以基于RBF神经网络算法的VSG转动惯量自调节为例)
  19. 嵌入式linux开发04-roottfs移植
  20. html5中分镜图文脚本,抖音分镜脚本这样做 轻松打造100w+粉丝大号

热门文章

  1. matlab应用于体育彩票上,马尔科夫预测法在体育彩票“排列三”中的应用
  2. urllib3如何安装的三种办法
  3. R语言h2o深度学习分类
  4. linux自带视频播放VLC,如何将VLC媒体播放器设置为默认视频播放器?
  5. oracle的dba_ segment,SEGMENT_TYPE TEMPORARY
  6. 意想不到,这个神奇的bug让我加班到深夜
  7. 华为暑期实习一面凉经
  8. 关于华为实习的一两点感触
  9. java android 计算两个日期之间的天数
  10. wxPython 4.2.0 发布