帮助开始分配特定位
所以我的课程有一个问题,我在开始时遇到困难。我并不是要求人们为我解决这个问题,我只是希望有人能朝正确的方向推动。我需要在 C 中创建一个函数,当给定任何 32 位整数时,它会返回一个整数,其中从最小 sig 位开始,每 4 位都设置为 1。我明白它最终应该是什么样子,但开始时我迷失了方向。我们不允许使用任何 for 循环或条件,只能使用标准的按位和逻辑运算符(! ~ & ^ | + <<>>)。再说一次,我并不是要求任何人为我做这件事,我只是想要一些帮助让我思考正确的轨道。我在这里和其他页面上看到了一些其他帖子,但似乎没有一个点击。我知道你可以将 1 位移位到某个位置 x<<3 但超出这个位置我就陷入困境了。任何帮助将不胜感激。
So I have a problem for my class that I am having trouble getting started on. I am not asking people to do the problem for me, I just would like any nudge in the right direction. I need to create a function in C that when given any 32 bit integer it returns an integer where every 4th bit is set to a 1 starting at the least sig bit. I understand what it is supposed to look like in the end, but getting started I am lost on. We are not allowed to use any for loops or conditionals, just the standard bitwise and logical operators(! ~ & ^ | + << >>). Once again, I am not asking anyone to do this for me, I just would like some help in getting me thinking on the right track. I have seen some of the other posts on here and on other pages, but none seem to click. I understand that you can bitshift a 1 into a certain place x<<3 but going beyond that I am stuck. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,这主要是开玩笑的。以下是按位运算符的功能列表:
~
):切换一位,0 到 1 和 1 到 0&
):设置一位如果两个操作数都设置了该位置的位|
):设置一个位 如果任一操作数设置了该位置的位^
):设置如果正好一点的话一点点该位置设置在两个操作数之间<<
和>>
):沿给定方向将每一位移动指定的量。左移时,最低有效位将添加零。右移时,如果值是无符号或正数,则会添加零。以下是一些值得了解的按位技巧:
按位左移一位与乘以二相同。按位右移 1 与除以 2 并向下舍入相同。
2 的所有幂都恰好有一个 1 位。要查看一个数字是否为 2 的幂,您可以执行以下操作:
return !(x & (x - 1)) && x
例如,
x = 16
,它是 2 的幂。然后,x - 1 = 15
,因此要进行 AND 运算的值为00010000
和00001111
。由于两个操作数中至少一个的每个位位置都有零,因此结果为零。!0
为 true,因此请检查x != 0
是否成立。由于16 != 0
,该语句返回 true。如果您尝试使用不是 2 的幂且不是零的数字,则x & (x - 1)
检查将始终为真。太酷了!另一个提示:由于
0 ^ 0 = 0
和1 ^ 1 = 0
,您可以使用 XOR 来查看哪些位发生了变化。例如,如果您有两个字节并且想要查看其间更改的位,则这两个字节的异或将为您提供所有已更改位的位置 1。Ok, that was mostly facetious. Here's a list of what the bitwise operators do:
~
): Toggle a bit, 0 to 1 and 1 to 0&
): Set a bit if the bit in that position is set in both operands|
): Set a bit if the bit in that position is set in either operand^
): Set a bit if exactly one bit in that position is set between the two operands<<
and>>
): Move each bit over the specified amount in the given direction. When shifting left, zeros are added to the least significant bit. When shifting right, a zero will be added if the value is either unsigned or positive.Here are some bitwise tricks that are good to know:
A bitwise shift left by one is the same as multiplying by two. A bitwise shift right by one is the same as dividing by two, and rounding down.
All powers of two have exactly one 1 bit. To see if a number is a power of two, you can do this:
return !(x & (x - 1)) && x
As an example, say
x = 16
, which is a power of two. Then,x - 1 = 15
, so the values to be ANDed are00010000
and00001111
. Since each bit position has a zero in at least one of the two operands, the result is zero.!0
is true, so check to see ifx != 0
. Since16 != 0
, the statement returns true. If you try it with a number that is not a power of two and not zero, then thex & (x - 1)
check will always be true. So cool!Another tip: since
0 ^ 0 = 0
and1 ^ 1 = 0
, you can use XOR to see what bits have changes. For example, if you have two bytes and want to see the bits that changed between then, the XOR of the two bytes will give you a 1 in the position of all bits that have changed.你能写下(十六进制)每 4 位设置为 1、所有其他位设置为 0 的 32 位整数吗?
现在,是否有一个操作可以应用于您的输入和这个幻数,它将输入的每 4 位设置为 1,但保留其他位?
Can you write down (in hex) the 32 bit integer that has every 4th bit set to 1, and all other bits set to 0?
Now, is there an operation you can apply to your input and this magic number, which sets every 4th bit of the input to 1, but leaves the other bits alone?
查看位掩码。
因此,您的情况的掩码(留下其他位不变)将是:
二进制:
10001000100010001000100010001000
十六进制:
88888888
Check out bitmasking.
So the mask for your case (leaving other bits unchanged) would be:
Binary:
10001000100010001000100010001000
Hex:
88888888