c++ - `(x+7)&~7`,把x增大为稍大于x的8的倍数。这怎么理解?

查看:96
本文介绍了c++ - `(x+7)&~7`,把x增大为稍大于x的8的倍数。这怎么理解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

stl源码剖析的第61页有上面的位运算,不过我不了解,求证明。

解决方案

7 = 00000111b
& is bitwise AND
so the x&7 has the only 3 low bits of x.


~7 = 11111000b

so the x&~7 will set x's 3 low bits to 0, and it is the multiple of 8.


(x+n-1)&(n-1),把x增大稍大于x的n的倍数,前提是n必须为 2^m
because only 2^m-1 has the type : high bits must be 1, low bits must be 0, not intersected.

subnet mask 子网掩码

这篇关于c++ - `(x+7)&~7`,把x增大为稍大于x的8的倍数。这怎么理解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆