C语言 我如何获得int的低8位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3270307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:55:04  来源:igfitidea点击:

How do i get the lower 8 bits of int?

c

提问by Laz

Lets say I have an int variable n = 8. On most machines this will be a 32 bit value. How can I only get the lower 8 bits (lowest byte) of this in binary? Also how can i access each bit to find out what it is? My question is related to C

假设我有一个 int 变量 n = 8。在大多数机器上,这将是一个 32 位值。我怎样才能以二进制形式获得它的低 8 位(最低字节)?另外我如何访问每一位以找出它是什么?我的问题与C有关

回答by Eli Bendersky

unsigned n = 8;
unsigned low8bits = n & 0xFF;

Note a few things:

请注意以下几点:

  1. For bitwise operations, always use the unsignedtypes
  2. Bits can be extracted from numbers using binary masking with the &operator
  3. To access the low 8 bits the mask is 0xFFbecause in binary it has its low 8 bits turned on and the rest 0
  4. The low 8 bits of the number 8 are... 8 (think about it for a moment)
  1. 对于按位运算,始终使用unsigned类型
  2. 可以使用带有&运算符的二进制掩码从数字中提取位
  3. 要访问低 8 位,掩码是0xFF因为在二进制中,它的低 8 位打开,其余为 0
  4. 数字8的低8位是... 8(想一想)


To access a certain bit of a number, say the kth bit:

要访问数字的某个位,请说第kth 位:

unsigned n = ...;
unsigned kthbit = (1 << k) & n;

Now, kthbitwill be 0 if the kth bit of nis 0, and some positive number (2**k) if the kth bit of nis 1.

现在,kthbit如果k第0位n为 0,则为 0,2**k如果第k1 位n为 1 ,则为某个正数 ( ) 。

回答by Mark Rushakoff

Use bitwise arithmetic to mask off the lowest 8 bits:

使用按位算术屏蔽掉最低的 8 位:

unsigned char c = (x & 0xFF);

To access the nth lowest bit, the equation is (x & (1 << n))(n of zero indicates the least significant bit). A result of zero indicates the bit is clear, and non-zero indicates the bit is set.

要访问第 n 个最低位,等式是(x & (1 << n))(n 为零表示最低有效位)。结果为零表示该位已清除,非零表示该位已设置。

回答by njsf

The best way is to use the bit logical operator & with the proper value.

最好的方法是使用具有适当值的位逻辑运算符 &。

So for the lower 8 bits:

所以对于低 8 位:

n & 0xFF; /* 0xFF == all the lower 8 bits set */

Or as a general rule:

或者作为一般规则:

n & ((1<<8)-1) /* generate 0x100 then subtract 1, thus 0xFF */

You can combine with the bit shift operator to get a specific bit:

您可以结合位移运算符来获取特定位:

(n & (1<<3))>>3; 
  /* will give the value of the 3rd bit - note the >>3 is just to make the value either 0, or 1, not 0 or non-0 */

回答by user11977

You can test if a particular bit is set in a number using << and &, ie:

您可以使用 << 和 & 测试是否在数字中设置了特定位,即:

if (num & (1<<3)) ...

if (num & (1<<3)) ...

will test if the fourth bit is set or not.

将测试是否设置了第四位。

Similarly, you can extract just the lowest 8 bits (as an integer) by using & with a number which only has the lowest 8 bits set, ie num & 255or num & 0xFF(in hexadecimal).

类似地,您可以通过将 & 与一个仅设置了最低 8 位的数字一起使用来提取最低 8 位(作为整数),即num & 255num & 0xFF(以十六进制表示)。