1s和2s补数的负数 [英] 1s and 2s complement of a negative number

查看:266
本文介绍了1s和2s补数的负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找到了关于如何找到1(翻转正二进制位)和2(翻转正二进制二进制位并加1)补码的所有答案,似乎都无法回答我的问题.

我的家庭作业要求找到负数的补码.因此,与其开始以正数开始,而不是找出负数是什么,我给了我一个负数并要求找到它的补数. /p>

一个愚蠢的想法是,我是否找到正值二进制值,然后翻转这些位以获得我的负数,然后再次翻转它以找到我的负数的1s补码?

解决方案

起始号码是正数还是负数都没有关系-在二进制补码系统中,-x~x + 1相同.如果原始数字为正,则结果为负;如果原始数字为负,则结果为正. 8位示例-二进制表示2的补码二进制:

x (decimal) | -x (decimal) | x (binary) | -x (binary) | ~x (binary) | ~x+1 (binary)
------------+--------------+------------+-------------+-------------+---------------
     5      |      -5      |  0000 0101 |  1111 1011  |  1111 1010  |  1111 1011
    -5      |       5      |  1111 1011 |  0000 0101  |  0000 0100  |  0000 0101
   110      |    -110      |  0110 1110 |  1001 0010  |  1001 0001  |  1001 0010
   -38      |      38      |  1101 1010 |  0010 0110  |  0010 0101  |  0010 0110

请注意-x (binary)列和~x+1 (binary)列相同.

如果您需要获取数字的1的补码,则只需~x.

All answers I seem to find on how to find the 1s (flip the bits of the positive) and the 2s (flip the bits of the positive binary and add 1) complement doesn't seem to answer my question.

My homework assignment asks to find the complement of a negative number.. So instead of starting out with a positive, and needed to find out what its negative is, I am given a negative number and asked to find its complement.

One silly thought is, do I find the positive value binary value, then flip the bits to get my negative number, then flip it again to find my 1s complement of the negative number??

解决方案

It doesn't matter if your starting number is positive or negative - in a two's complement system, -x is the same as ~x + 1. If your original number is positive, the result will be negative, and if the original number is negative, the result will be positive. 8-bit example - binary means 2's complement binary:

x (decimal) | -x (decimal) | x (binary) | -x (binary) | ~x (binary) | ~x+1 (binary)
------------+--------------+------------+-------------+-------------+---------------
     5      |      -5      |  0000 0101 |  1111 1011  |  1111 1010  |  1111 1011
    -5      |       5      |  1111 1011 |  0000 0101  |  0000 0100  |  0000 0101
   110      |    -110      |  0110 1110 |  1001 0010  |  1001 0001  |  1001 0010
   -38      |      38      |  1101 1010 |  0010 0110  |  0010 0101  |  0010 0110

Note the -x (binary) column and the ~x+1 (binary) column are the same.

If you need to get the 1's complement of a number, that's just ~x.

这篇关于1s和2s补数的负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆