简体   繁体   English

如何在Android中进行1的补码和加1?

[英]How to do 1's complement and add 1 in Android?

I am develop an application in Android . 我正在用Android开发应用程序。 I receive the value from network and convert it by 1's complement. 我从网络接收到值并将其转换为1的补码。


For Example : 例如 :

The value is 247 . 该值为247

I convert the 247 to the binary: 1111 0111 我将247转换为二进制文件: 1111 0111

After the 1's complement add 1 , the value is 0000 1001 . 1的补码加1之后,值是0000 1001

Turn the the value 0000 1001 to the Decimal is 9 . 将值0000 1001转到小数为9

It mean the -9 in my application , and that is the value what I want. 这意味着我的应用程序中的-9 ,这就是我想要的值。


I try to use Integer.toBinaryString for convert the 247 to the 1111 0111 . 我尝试使用Integer.toBinaryString247转换为1111 0111 And use for loop to convert the 0 to 1 and 1 to 0 . 并使用for loop0 to 1转换0 to 1并将1 to 0转换1 to 0 But it seems not a good method... 但这似乎不是一个好方法...

What should I do to implement the above example ? 我应该怎么做才能实现上述示例?

Multiply by -1 乘以-1

2's complement already means ADD 1. If you just invert the number it is called 1's complement. 2的补码已经表示加1。如果您将数字取反,则称为1的补码。

But the numbers are stored in binary already. 但是数字已经以二进制形式存储。 If you get the number 247 it means you are treating it as UNSIGNED and you want to treat it as SIGNED. 如果得到数字247,则表示您将其视为UNSIGNED,并且要将其视为SIGNED。 If it is 8 bits, treat it as a 'signed char' 如果是8位,则将其视为“签名字符”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM