[英]Activate some bits of a number in C
I need to implement the function int activate_bits(int a, int left, int right) that should 'activate' all the bits to the left of left and to the right of right on the number a (excluding the bits left and right).我需要实现 function int activate_bits(int a, int left, int right)应该“激活”数字 a 左侧和右侧的所有位(不包括左右位)。
So far I've came up with this:到目前为止,我想出了这个:
int activate_bits(int a, int left, int right)
{
int n, j, mask, masked_a;
for (n = 1; n < left; n++) {
mask = 1 << n;
masked_a = a | mask;
}
for (j = 31; j > right; j--) {
mask = 1 >> j;
masked_a = a | mask;
}
return masked_a;
}
Can anyone help me to why the masked_a it's not correct or fix my faulty code?谁能帮我解释为什么masked_a不正确或修复我的错误代码?
I suspect you need to be setting masked_a
to a
before looping:我怀疑您需要在循环之前将
masked_a
设置为a
:
int activate_bits(int a, int left, int right)
{
int n, j, mask, masked_a;
masked_a = a;
for (n = 0; n < left; n++) {
mask = 0x80000000 >> n;
masked_a = masked_a | mask;
}
for (j = 0; j < right; j++) {
mask = 1 << j;
masked_a = masked_a | mask;
}
return masked_a;
}
EDIT: I've updated the loops to what I think are correct, although that is open to interpretation depending on what the expected answer is.编辑:我已经将循环更新为我认为正确的内容,尽管这取决于预期的答案是什么可以解释。 Apologies for the earlier incorrect response, I was just pointing out the glaring error and that should probably have been a comment.
为早先的错误回复道歉,我只是指出了明显的错误,这可能应该是一个评论。
left
and right
in original code since left is highest bit and right is lowest bit.right
切换,因为left
是最高位,右边是最低位。1>>j
to 1<<j
1>>j
更改为1<<j
Assume your left and right is starting from 1假设你的左右是从 1 开始的
int activate_bits(int a, int left, int right)
{
int n, j, mask, masked_a = a;
for (n = 0; n < right - 1; n++) {
mask = 1 << n;
masked_a = a | mask;
}
for (j = 31; j > 31 - left + 1; j--) {
mask = 1 << j;
masked_a = a | mask;
}
return masked_a;
}
some functions to set, reset and write new value一些设置、重置和写入新值的功能
unsigned set_bits(unsigned a, unsigned pos, unsigned size)
{
unsigned mask = ((1 << size) - 1) << pos;
return a | mask;
}
unsigned reset_bits(unsigned a, unsigned pos, unsigned size)
{
unsigned mask = ((1 << size) - 1) << pos;
return a & ~(mask);
}
unsigned write_bits(unsigned a, unsigned newVal, unsigned pos, unsigned size)
{
unsigned mask = ((1 << size) - 1);
newVal &= mask;
mask <<= pos;
return (a & ~(mask)) | newVal << pos;
}
to make your units:制作你的单位:
right = pos
left = pos + size - 1;
I had the same question.我有同样的问题。 I've tried the solutions above, and none of them worked for me, since I think no one understood what you wanted exactly.
我已经尝试了上面的解决方案,但没有一个对我有用,因为我认为没有人完全理解你想要什么。 I know it's been a year, but this worked for me, and I believe it's pretty simple:
我知道已经一年了,但这对我有用,我相信这很简单:
int activate_bits(int a, int left, int right) {
left = 31 - left;
int i;
for (i = 0; i < left; i++) {
a = a | (0x80000000 >> i);
}
for (i = 0; i < right; i++) {
a = a | (1 << i);
}
return a;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.