这种大小调整是如何工作的 [英] How is this size alignment working

查看:113
本文介绍了这种大小调整是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够理解下面的code相对于所提供的评论。这是什么code呢,而且这将是相当于code为 8对齐

I am not able to understand the below code with respect to the comment provided. What does this code does, and what would be the equivalent code for 8-aligned?

/* segment size must be 4-aligned */
attr->options.ssize &= ~3;

在这里, ssize unsigned int型的键入

推荐答案

由于4为100,对准以4字节边界的任何值(即4的倍数)将具有最后两个比特设置为零。

Since 4 in binary is 100, any value aligned to 4-byte boundaries (i.e. a multiple of 4) will have the last two bits set to zero.

二进制3为11,和〜3是这些位的位反,即,... 1111100。执行位与该值将保持每一点相同的,除了最后两个将被清除(比特及1 ==位,和比特及0 == 0)。这为我们提供了一个下一个较低或相等值,该值是4的倍数。

3 in binary is 11, and ~3 is the bitwise negation of those bits, i.e., ...1111100. Performing a bitwise AND with that value will keep every bit the same, except the last two which will be cleared (bit & 1 == bit, and bit & 0 == 0). This gives us a the next lower or equal value that is a multiple of 4.

要8(二进制1000)做同样的操作,我们需要清除最低的三个位。我们可以做到这一点与二进制111的按位求反,即7〜

To do the same operation for 8 (1000 in binary), we need to clear out the lowest three bits. We can do that with the bitwise negation of the binary 111, i.e., ~7.

这篇关于这种大小调整是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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