- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
1.请考虑以下事项:
unsigned int a, b;
b = a >> ((sizeof a) * CHAR_BIT);
/* or 2nd operand greater than ((sizeof a) * CHAR_BIT) */
这是定义的、未定义的行为还是依赖于实现的行为?
2。还有一个子问题:
案例a
是signed int
并且它的移位小于其位长度,是有符号位移实现定义或未定义的行为。在这两种情况下:
a >> 5
a << 5
EDIT 已编辑问题
最佳答案
1.
来自 C99 标准,第 6.5.7 节:
The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
所以它是未定义的。
2.
来自同一部分:
The result of
E1 << E2
isE1
left-shiftedE2
bit positions; vacated bits are filled with zeros. IfE1
has an unsigned type, the value of the result isE1
x 2E2
, reduced modulo one more than the maximum value representable in the result type. IfE1
has a signed type and nonnegative value, andE1
x 2E2
is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.The result of
E1 >> E2
isE1
right-shiftedE2
bit positions. IfE1
has an unsigned type or ifE1
has a signed type and a nonnegative value, the value of the result is the integral part of the quotient ofE1
/ 2E2
. IfE1
has a signed type and a negative value, the resulting value is implementation-defined.
所以对于左移,如果 a
是明确定义的已签名且为正。如果 a
则未定义有符号且为负。
对于右移,如果 a
是明确定义的已签名且为正。如果a
,它是实现定义的有符号且为负。
关于c - a >> ((sizeof a) * CHAR_BIT) 是定义的,UB 还是 IDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7333025/
我对 limit.h 中的 CHAR_BIT 感到困惑。我读过一些文章说宏 CHAR_BIT 是为了可移植性。在代码中使用宏而不是像 8 这样的魔数(Magic Number),这是合理的。但是lim
CHAR_BIT 定义每个字符的位数。但它是一个旧的宏。类似的宏已在最近的 C++ 标准中被替换。 C++11/14/17 中是否有更现代的 CHAR_BIT 替代品? 最佳答案 非符号位数由 std
引用计算整数绝对值 (abs) 的代码而不从 http://graphics.stanford.edu/~seander/bithacks.html 分支: int v; // w
我只是 checking an answer并意识到 CHAR_BIT没有像我期望的那样由标题定义,甚至没有 #include , 在较新的 GCC 上。 我真的需要#include 只是为了获得
我有一个用 C 语言实现的位 vector ,我想避免除法和模数运算,并用(更快的)位运算替换它们。因此,我将位放入字节(字符)中,并需要索引到这些数组中,然后取出一些位。我的实现如下所示: #inc
ISO C 标准要求 CHAR_BIT 至少为 8。 POSIX 要求 CHAR_BIT 等于 8,并且(几乎?)所有使用 octets 的网络和通信标准,是否有任何当代C 实现,其中 CHAR_BI
根据 N4140(C++11 工作草案): The fundamental storage unit in the C ++ memory model is the byte. A byte is a
似乎没有任何库函数可以计算类型的位大小。 我认为这可以通过以下方式完成吗? #include template size_t Size_In_Bits(){ return sizeof(T
在 Tanenbaum 的操作系统设计和实现中,第 154 页说位图中的每个 NR_SYS_PROCS(32) 都有一个位。 并且在 minix/kernel/table.c 的末尾,有一个检查以确保
在 POSIX 基本原理中有一个说明,强制 CHAR_BIT 为 8 是为了保持与 C99 的一致性而不丢弃套接字/网络所必需的让步,但我从来没有看到冲突到底是什么的解释。有没有人有轶事或引文说明为什
我正在考虑一个非常特殊的(现在没用的)处理器,叫做 saturn . 这个 CPU 的一个奇怪特征是它的元素单位是半字节(4 位),而不是字节(8 位)。这不仅仅是装饰性的,任何指针值都以半字节表示。
我手边没有任何带有 CHAR_BIT > 8 的平台。 所以我更改了文件limits.h中的相关预处理器定义: #define CHAR_BIT 16 /* number
1.请考虑以下事项: unsigned int a, b; b = a >> ((sizeof a) * CHAR_BIT); /* or 2nd operand greater than ((siz
如果目标系统上的 CHAR_BIT == 8(大多数情况下),则很容易屏蔽掉单个字节: unsigned char lsb = foo & 0xFF; 但是,在一些系统和 C 实现中,CHAR_BIT
如果目标系统上的 CHAR_BIT == 8(大多数情况下),则很容易屏蔽掉单个字节: unsigned char lsb = foo & 0xFF; 但是,在一些系统和 C 实现中,CHAR_BIT
在 C99 的 7.19.7.1 节中,我们有: If the end-of-file indicator for the input stream pointed to by stream is n
C# 是否有类似于 C++ 的 CHAR_BIT 的东西? ? 更新: 基本上,我试图在没有分支的情况下计算 abs,这是 C++ 版本: // Compute the integer absolut
是否存在机器(或编译器),其中 sizeof(char) != 1? C99 标准 是否规定sizeof(char) 在标准合规性实现中必须正好为 1?如果有,请给我章节编号和引用。 更新:如果我有一
我是一名优秀的程序员,十分优秀!