简体   繁体   English

Bit Hack - 四舍五入到8的倍数

[英]Bit Hack - Round off to multiple of 8

can anyone please explain how this works (asz + 7) & ~7; 任何人都可以解释这是如何工作的(asz + 7)&〜7; It rounds off asz to the next higher multiple of 8. 它将asz舍入到8的下一个更高倍数。

It is easy to see that ~7 produces 11111000 (8bit representation) and hence switches off the last 3 bits ,thus any number which is produced is a multiple of 8. 很容易看出~7产生11111000(8位表示)并因此关闭最后3位,因此产生的任何数字是8的倍数。

My question is how does adding asz to 7 before masking [edit] produce the next higher[end edit] multiple of 8 ? 我的问题是如何在屏蔽[edit]之前将asz添加到7以产生8的下一个更高[end edit]倍数? I tried writing it down on paper 我试着在纸上写下来

like : 喜欢 :

1 + 7 = 8  = 1|000 (& ~7) -> 1000
2 + 7 = 9  = 1|001 (& ~7) -> 1000
3 + 7 = 10 = 1|010 (& ~7) -> 1000
4 + 7 = 11 = 1|011 (& ~7) -> 1000
5 + 7 = 12 = 1|100 (& ~7) -> 1000
6 + 7 = 13 = 1|101 (& ~7) -> 1000
7 + 7 = 14 = 1|110 (& ~7) -> 1000
8 + 7 = 15 = 1|111 (& ~7) -> 1000

A pattern clearly seems to emerge which has been exploited .Can anyone please help me it out ? 似乎已经出现了一种被剥削的模式。任何人都可以帮助我吗?

Thank You all for the answers.It helped confirm what I was thinking. 谢谢大家的答案。这有助于确认我的想法。 I continued the writing the pattern above and when I crossed 10 , i could clearly see that the nos are promoted to the next "block of 8" if I can say so. 我继续写上面的模式,当我越过10时,我可以清楚地看到,如果我可以这样说,那么nos会被提升到下一个“8块”。

Thanks again. 再次感谢。

Well, if you were trying to round down , you wouldn't need the addition. 好吧,如果你试图向下舍入,你就不需要添加了。 Just doing the masking step would clear out the bottom bits and you'd get rounded to the next lower multiple. 只需执行屏蔽步骤就可以清除底部位,然后将四舍五入到下一个较低的位数。

If you want to round up , first you have to add enough to "get past" the next multiple of 8. Then the same masking step takes you back down to the multiple of 8. The reason you choose 7 is that it's the only number guaranteed to be "big enough" to get you from any number up past the next multiple of 8 without going up an extra multiple if your original number were already a multiple of 8. 如果你想要向上舍入,首先你必须添加足够的“过去”下一个8的倍数。然后相同的屏蔽步骤将你带回到8的倍数。你选择7的原因是它是唯一的数字如果您的原始数字已经是8的倍数,那么保证“足够大”可以让您从8的下一个倍数上升到任何数字而不会增加额外倍数。

In general, to round up to a power of two: 一般来说,要达到两个幂:

unsigned int roundTo(unsigned int value, unsigned int roundTo)
{
    return (value + (roundTo - 1)) & ~(roundTo - 1);
}

It's actually adding 7 to the number and rounding down . 它实际上是将7的数量和向下舍入。

This has the desired effect of rounding up to the next multiple of 8. (Adding +8 instead of +7 would bump a value of 8 to 16.) 这具有向上舍入到8的下一个倍数的期望效果。(添加+8而不是+7将使值8到16)。

The +7 isn't to produce an exact multiple of 8, it's to make sure you get the next highest multiple of eight. +7不会产生8的精确倍数,这是为了确保你获得8的下一个最高倍数。

edit: Beaten by 16 seconds and several orders of quality. 编辑:打16秒和几个质量订单。 Oh well, back to lurking. 哦,好吧,回到潜伏。

Well, the mask would produce an exact multiple of 8 by itself. 好吧,面具本身会产生8的精确倍数。 Adding 7 to asz ensures that you get the next higher multiple. 将7添加到asz可确保您获得下一个更高的倍数。

没有+7,它将是8或小于或等于原始数字的最大倍数

Adding 7 does not produce a multiple of 8. The multiple of 8 is produced by anding with ~7. 添加7不会产生8的倍数.8的倍数是通过~7产生的。 ~7 is the complement of 7, which is 0xffff fff8 (except using however many bits are in an int). ~7是7的补码,它是0xffff fff8(除非使用多个位在int中)。 This truncates, or rounds down. 这会截断或向下舍入。

Adding 7 before doing that insures that no value lower than asz is returned. 在执行此操作之前添加7可确保不返回低于asz的值。 You've already worked out how that works. 你已经弄清楚它是如何工作的。

Uhh, you just answered your own question??? 呃,你刚回答了自己的问题??? by adding 7, you are guaranteeing the result will be at or above the next multiple of 8. truncating then gives you that multiple. 通过添加7,您保证结果将等于或高于下一个倍数8.截断然后给你那个倍数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM