TWO’S COMPLEMENT NOTATION

本文介绍如何使用二进制补码表示负数,并解释了补码加法的工作原理。通过翻转位并加一的操作,可以轻松实现负数的二进制表示,使得加法运算无需额外步骤即可正确执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Two's complement notation is a way to encode negative numbers into ordinary binary, such that addition still works. Adding −1 + 1 should equal 0, but ordinary addition gives the result of 2 or −2 unless the operation takes special notice of the sign bit and performs a subtraction instead. Two's complement results in the correct sum without this extra step.


To represent a negative number using two’s complement notation:

  1. Start with the binary representation of the positive version of the number.

  2. Complement all the bits (turn the ones into zeros and the zeros into ones).

  3. Add one to the result.


For example, the binary notation for the number 9 is 00001001. To represent –9 in two’s complement notation, flip the bits (11110110) and then add 1. The two’s complement for –9 is 11110110 + 1 = 11110111. The binary notation for the number 2 is 00000010. The two’s complement for –2 would be 11111101 + 1 =11111110.


Don’t worry about the details of binary representation and arithmetic. What’s important to remember is that the computer uses one notation for positive-only numbers and a different notation for numbers that can be positive or negative. Both notations allow a byte to take on one of 256 different values. The positives-only scheme allows values ranging from 0 to 255. The two’s complement scheme allows a byte to take on values ranging from –128 to 127. Note that both of these ranges contain exactly 256 values. If the 2 bytes are unsigned(never allowed to hold a negative value), they can hold values ranging from 0 to 65,535. If the 2 bytes are signed(allowed to hold both positive and negative values), they can hold values ranging from –32,768 to 32,767.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值