c 获取整数的第n个字节

标签 c byte shift bit-shift

我知道你可以使用

获取第一个字节
int x = number & ((1<<8)-1);

int x = number & 0xFF;

但我不知道如何获取整数的第 n 个字节。 例如,1234 为 32 位整数 00000000 00000000 00000100 11010010 我怎样才能获得所有这些字节?第一个是 210,第二个是 4,最后两个是 0。

最佳答案

int x = (number >> (8*n)) & 0xff;

其中 n 为 0 表示第一个字节,1 表示第二个字节,依此类推。

关于c 获取整数的第n个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39326283/

相关文章:

javascript - 是否在 JavaScript 中按住了 shiftkey

c - Galois LFSR 代码解释

c++ - 如何为并行进程正确选择 rng seed

c - 如何使用 fread 将 char 保存到二维数组?

Java 和无符号字节

string - golang中字符串,字节和 rune 的行为

android - 字节数组音频连接Android

c++ - 嵌入式系统状态模式: Storing information related to asynchronous events

c - 当字符串为数字时C语言

pandas - 如何在 tensorflow/keras 中移动像 pandas.shift 这样的张量? (无需将最后一行移到第一行,如 tf.roll)