获取低阶N位 [英] Getting Lower-Order N Bits
本文介绍了获取低阶N位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有任何方法可以获取任何整数的低阶 n 位(其中 n 可以是1到32之间的任何数字)而无需预先计算32位掩码,每个订单一个,并使用&
运算符?我也不希望使用具有两个幂的%
,而只是按位运算.
Is there any way you can get any integer's lower-order n bits (where n can be any number between 1 and 32) without previously pre-computing 32 bitmasks, one for each order, and using the &
operator? I also don't want to be using %
with powers of two, just bitwise operations.
例如,假设用户输入一个整数 Num
和另一个整数 ShiftCount
,值的范围是1到32我想将操作 Num>>中丢失的位存储在第三个变量中.ShiftCount
.
Say, for example, that a user enters an integer Num
and another integer ShiftCount
with a value ranging from 1 to 32. I want to store in a third variable the bits that are lost in the operation Num >> ShiftCount
.
推荐答案
类似于 Num&((1<< ShiftCount)-1)
?
这篇关于获取低阶N位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文