[英]How do I cut off first bits in an unsigned long with bitwise?
I have m = 0x401e000000000000
and I want to get f = 0xe000000000000
.我有
m = 0x401e000000000000
并且我想得到f = 0xe000000000000
。 Using bitwise operators, how would I do that in C?使用按位运算符,我将如何在 C 中执行此操作? I used
f = (m & 0xFFFFFFFFFFFFF);
我用
f = (m & 0xFFFFFFFFFFFFF);
but I just get 0.但我只得到0。
When run in IDEOne , it works在IDEOne中运行时,它可以工作
#include <stdio.h>
int main(void) {
unsigned long m = 0x401e000000000000;
unsigned long f = m & (0xFFFFFFFFFFFFF); // Expect value = 0xe000000000000.
printf("Result f = 0x%0lX\n", f);
return 0;
}
Success #stdin #stdout 0s 5416KB
Result f = 0xE000000000000
You can use also the >>
<<
operators, the "12" in the code is the bit positions you you want to shift.您也可以使用
>>
<<
运算符,代码中的“12”是您要移动的位位置。 The <<
operator shifts its left-hand operand left by the number of bits defined by its right-hand operand. <<
运算符将其左侧操作数向左移动其右侧操作数定义的位数。 The left-shift operation discards the high-order bits that are outside the range of the result type and sets the low-order empty bit positions to zero, then you can use the >>
operand to restore the scale.左移操作舍弃结果类型范围之外的高位,并将低位空位位置置零,然后可以使用
>>
操作数恢复比例。
#include <stdio.h>
int main()
{
unsigned long m = 0x401e000000000000;
m = (m<<12);
m = (m>>12);
printf("Result f = 0x%0lX\n", m);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.