C语言 如何在C中获得int的二进制补码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36028198/
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
How to get two's complement of an int in C?
提问by Thahleel Abid
How do you get the Two's complement for a int in C?
你如何获得 C 中 int 的二进制补码?
Say for example I had an int such as -254, how would I go about converting this to 100000010?
比如说我有一个 -254 这样的整数,我将如何将它转换为 100000010?
Is there any way to pull out the Two's complement value from the integer variable, as I know that in C, the ints are stored in Two's comp?
有没有办法从整数变量中提取出Two的补码值,因为我知道在C中,int存储在Two的comp中?
回答by Lundin
know that in C, the ints are stored in Two's comp
知道在 C 中,整数存储在 Two 的 comp 中
Not guaranteed, but in practice every computer uses two's complement.
不能保证,但实际上每台计算机都使用二进制补码。
Is there any way to pull out the Two's complement value from the integer variable
有没有办法从整数变量中提取出二进制补码值
It is already in two's complement format, so it is unclear what you are asking. It would seem you are asking how to print a variable in binary format?
它已经是二进制补码格式,所以不清楚你在问什么。您似乎在问如何以二进制格式打印变量?
int data = -254;
const size_t BITS = 8*sizeof(data);
char bin_str[BITS+1];
for(unsigned int i=0; i<BITS; i++)
{
unsigned int mask = 1u << (BITS - 1 - i);
bin_str[i] = (data & mask) ? '1' : '0';
}
bin_str[BITS] = 'int sm2tc(int x) {
int m = x >> 31;
return (~m & x) | (((x & 0x80000000) - x) & m);
}
';
回答by luser droog
If you're operating on unsigned ints then you can invert the bits ~and add 1to yield the 2s complement value. x=(~y)+1;If your machine uses a 2s complement representation for signed intthen this should convert (by the implementation's definition) to the proper signed intvalue.
如果您在unsigned ints 上操作,那么您可以反转位~并相1加以产生 2s 补码值。x=(~y)+1;如果您的机器使用 2s 补码表示,signed int那么这应该(根据实现的定义)转换为正确的signed int值。
The C language itself is a little vague in its guarantees in this area. To work portably on the bitwise representation of an object you should use an unsignedtype.
C 语言本身在这方面的保证方面有点模糊。要可移植地处理对象的按位表示,您应该使用unsigned类型。
回答by Gehan Fernando
You can convert signed-magnitude to two's complement by subtracting the number from 0x80000000 if the number is negative. This will work for a 32-bit integer on a machine using two's complement to represent negative values, but if the value is positive this will result in a two's complement negation. A right shift of a two's complement negative number will shift in one's, we can utilize this to make a mask to select between the original value, or the conversion of a signed-magnitude negative value to a two's complement negative value.
如果数字为负,您可以通过从 0x80000000 中减去数字来将有符号幅度转换为二进制补码。这将适用于使用二进制补码表示负值的机器上的 32 位整数,但如果该值为正,这将导致二进制补码取反。一个二进制补码负数的右移会移动一个,我们可以利用它来制作一个掩码,在原始值之间进行选择,或者将一个有符号大小的负值转换为一个二进制补码负值。
##代码##Original Code posted by Apriori
Apriori发布的原始代码
回答by hamzamuhammad
Actually, there is an easy way to do this. Simply convert your number to a binary string, and convert that string back to an integer.
实际上,有一种简单的方法可以做到这一点。只需将您的数字转换为二进制字符串,然后将该字符串转换回整数。
Use itoa, which converts a number to its given base in string form.
使用itoa,它将数字转换为字符串形式的给定基数。
http://www.cplusplus.com/reference/cstdlib/itoa/
http://www.cplusplus.com/reference/cstdlib/itoa/
Then, simply use the familiar atoi that converts it back to an int.
然后,只需使用熟悉的 atoi 将其转换回 int。

