Linux网络编程中 按位操作

写Linux程序经常会看到&和|按位操作符号,具体就是内核提供的宏之间进行按位操作。

他们具体什么意思?

我们在macOS中看到poll.h的事件的宏定义:

#define POLLIN          0x0001          /* any readable data available */
#define POLLPRI         0x0002          /* OOB/Urgent readable data */
#define POLLOUT         0x0004          /* file descriptor is writeable */
#define POLLRDNORM      0x0040          /* non-OOB/URG data available */
#define POLLWRNORM      POLLOUT         /* no write type differentiation */
#define POLLRDBAND      0x0080          /* OOB/Urgent readable data */
#define POLLWRBAND      0x0100          /* OOB/Urgent data can be written */

/*
 * FreeBSD extensions: polling on a regular file might return one
 * of these events (currently only supported on local filesystems).
 */
#define POLLEXTEND      0x0200          /* file may have been extended */
#define POLLATTRIB      0x0400          /* file attributes may have changed */
#define POLLNLINK       0x0800          /* (un)link/rename may have happened */
#define POLLWRITE       0x1000          /* file's contents may have changed */

用到poll网络模型时候,我们会使用 POLLIN | POLLRDHUP | POLLERR 这样的操作,通过上面的源码可以看到这些宏是十六进制数,0x0001的二进制是1,0x0002是10,0x0004是100,0x0040是1000000,ox0200是1000000000,0x0400是10000000000,0x0800是100000000000,0x1000是1000000000000。

因此给某个文件描述符添加某些事件时会用到按位操作,在二进制位上进行运算,保证计算速度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾文繁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值