问题描述
我怎么会从符号 - 幅度转换成二进制补码。我不知道从哪里开始。任何帮助将是AP preciated。
我只可以使用下面的操作:!,〜,|,&安培;,^ +,>>,<<
/ *
* sm2tc - 从符号 - 幅度转换为二进制补码
*其中MSB是符号位
*例:sm2tc(0x80000005)= -5。
*
* /
INT sm2tc(INT X){返回2;
}
您可以通过为0x80000000减去号码,如果该数字为负符号 - 幅度转换成二进制补码。这将用于使用二的补重新present负值的机器上的32位整数工作,但如果该值是正的,这将导致一个二的补否定。一个二的补负数的右移将在一个人的移动,我们可以利用这种使掩模原始值,或一个符号 - 幅度表示负值的一个二的补码的负值转换之间进行选择。
INT sm2tc(INT X){
INT M = X - GT;> 31;
返回(〜M&安培; X)| (((X安培; 0x80000000的) - X)及米);
}
How would I convert from sign-magnitude to two's complement. I don't know where to start. Any help would be appreciated.I can only use the following operations:!,~,|,&,^,+,>>,<<.
/*
* sm2tc - Convert from sign-magnitude to two's complement
* where the MSB is the sign bit
* Example: sm2tc(0x80000005) = -5.
*
*/
int sm2tc(int x) {
return 2;
}
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.
int sm2tc(int x) {
int m = x >> 31;
return (~m & x) | (((x & 0x80000000) - x) & m);
}
这篇关于如何从符号 - 幅度转换为二的补的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!