Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置整数的前三个字节?在C++; 我想把C++的第一个三字节的整数设置为0。我尝试了这段代码,但是我的整数变量a没有改变,输出总是-63。我做错了什么 #include <iostream> #include <string> int main() { int a = 4294967233; std::cout << a << std::endl; for(int i = 0; i< 24; i++) { a |= (0 << (i+8)); std::cout << a << std::endl; } } #包括 #包括 int main() { INTA=4294967233; std::cout_C++_Bit Manipulation - Fatal编程技术网

如何设置整数的前三个字节?在C++; 我想把C++的第一个三字节的整数设置为0。我尝试了这段代码,但是我的整数变量a没有改变,输出总是-63。我做错了什么 #include <iostream> #include <string> int main() { int a = 4294967233; std::cout << a << std::endl; for(int i = 0; i< 24; i++) { a |= (0 << (i+8)); std::cout << a << std::endl; } } #包括 #包括 int main() { INTA=4294967233; std::cout

如何设置整数的前三个字节?在C++; 我想把C++的第一个三字节的整数设置为0。我尝试了这段代码,但是我的整数变量a没有改变,输出总是-63。我做错了什么 #include <iostream> #include <string> int main() { int a = 4294967233; std::cout << a << std::endl; for(int i = 0; i< 24; i++) { a |= (0 << (i+8)); std::cout << a << std::endl; } } #包括 #包括 int main() { INTA=4294967233; std::cout,c++,bit-manipulation,C++,Bit Manipulation,您需要&=而不是|=只需使用带掩码的按位and(&),就没有理由循环: a &= 0xFF000000; // Drops all but the third lowest byte a &= 0x000000FF; // Drops all but the lowest byte (感谢@JSF的更正) 如@black所述,为了使代码更具可读性,您可以使用自C++14: a &= 0xFF'00'00'00; // Drops all but the third lo

您需要
&=
而不是
|=

只需使用带掩码的按位and(
&
),就没有理由循环:

a &= 0xFF000000; // Drops all but the third lowest byte
a &= 0x000000FF; // Drops all but the lowest byte
(感谢@JSF的更正)

如@black所述,为了使代码更具可读性,您可以使用自C++14:

a &= 0xFF'00'00'00; // Drops all but the third lowest byte
a &= 0x00'00'00'FF; // Drops all but the lowest byte
如果您的真正意思是“第一个”而不是“最低”,那么请利用alias规则允许char alias任何内容的事实:

  int a = 4294967233;
  char* p=&a;
  ...
  p[0] = whatever you wanted there
  p[1] = whatever you wanted there
  p[2] = whatever you wanted there

联合也可能是一种选择

union my_type{
    int i;
    unsigned int u;
    ...
    unsigned char bytes[4];
};
...
my_type t;
t.u = 0xabcdefab;
t.bytes[0] = 5; // t.u = 0x05cdefab

0
左移所需的任何值仍然是
0
,并且ORing
0
仍然对原始值没有影响。您称之为“第一个”字节是什么?字节顺序取决于您机器的尾数。可能您要查找的表达式是“最高有效字节”
int
不需要足够大以容纳3个字节;它可以是16位宽。在桌面系统上,它通常是32或64位,但可移植代码不会做出这种假设。@YSC Endianness在这种情况下并不重要!@black它只是使用正确的单词来确保每个人都理解相同的内容。看看JSF的答案;)现在你的答案是0。这只是答案的一半。坦率地说,这方面的质量很低。你可能会发现使用
0xFF'00'00'00
更容易。你的第一个示例除了第三个最低值外,其他所有值都会下降。你的两个评论中的“三个”都是不合理的假设。如果你真的想删除“最低的三个”这应该是
a&=~0xFFFFFF;
,它假设
a
与平台的默认整数大小相同。如果
a
更大,则需要强制转换,例如
a&=~(unsigned long)0xFFFFFF;
与soon的回答不同,这将取决于endianness,因为您在内存中操作的字节布局实际上是平台相关的。您的评论暗示了“first”的含义与我的含义非常不同。我猜英语是不精确的。但我认为我对“first”一词的解释是更合理。因此,我的答案与soon的答案不同,不依赖于平台。很可能,OP实际上并不是指“第一”,而是指其他内容,这就是为什么我以这种方式开始我的答案。