8位二进制从0减1 [英] Subtracting 1 from 0 in 8 bit binary

查看:13
本文介绍了8位二进制从0减1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 8 位 int zero = 0b00000000; 和 8 位 int one = 0b00000001;根据二进制算术规则,

<块引用>

0 - 1 = 1(从下一个有效位借用 1).

如果我有:

int s = 零 - 一;s = -1;-1 = 0b1111111;

所有这些 1 是从哪里来的?没有什么可以借用的,因为 zero 变量中的所有位都是 0.

解决方案

这是一个很好的问题,与计算机如何表示整数值有关.

如果你要写出一个以十为底的负数,你只需写出常规数字,然后在它前面加上一个减号.但是,如果您在一台计算机中工作,所有内容都需要为零或一,那么您就没有任何减号.接下来的问题是你如何选择表示负值.

一种流行的方法是使用带符号的二进制补码形式.其工作方式是您使用 1 和 0 编写数字,除了这些 1 和 0 的含义在解释方式上与标准"二进制不同.具体来说,如果您有一个带符号的 8 位数字,则低七位的标准含义为 20、21、22等. 但是,最高有效位的含义发生了变化:不是表示 27,而是表示值 -27.

让我们看看数字 0b11111111.这将被解释为

<块引用>

-27 + 26 + 25 + 24 + 23 + 22 + 21 + 20

= -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= -1

这就是为什么这个比特集合代表-1.

还有另一种解释这里发生的事情的方法.鉴于我们的整数只有 8 位可以使用,我们知道没有办法表示所有可能的整数.如果您选择任何 257 个整数值,假设只有 256 种可能的位模式,则无法唯一表示所有这些数字.

为了解决这个问题,我们也可以说我们的整数值不是整数的真实值,而是整数模 256 的值.我们将存储的所有值都在0 和 255(含).

在这种情况下,0 - 1 是多少?它是 -1,但如果我们取模 256 并强制它为非负数,那么我们会得到 -1 = 255(模 256).你会如何用二进制写 255?它是 0b11111111.

如果您有兴趣,这里还有很多其他很酷的东西可以学习,所以我建议您阅读有符号和无符号二进制补码数.

作为一些练习:在这种格式下 -4 会是什么样子?-9 怎么样?

这些并不是您在计算机中表示数字的唯一方法,但它们可能是最流行的.一些较旧的计算机使用 平衡三进制数系统(尤其是 Setun 机器).还有 one 的补码格式,最近不太流行.p>

I have 8 bit int zero = 0b00000000; and 8 bit int one = 0b00000001; according to binary arithmetic rule,

0 - 1 = 1 (borrow 1 from next significant bit).

So if I have:

int s = zero - one; 
s = -1; 
-1 = 0b1111111;

where all those 1s are coming from? There are nothing to borrow since all bits are 0 in zero variable.

解决方案

This is a great question and has to do with how computers represent integer values.

If you’re writing out a negative number in base ten, you just write out the regular number and then prefix it with a minus sign. But if you’re working inside a computer where everything needs to either be a zero or a one, you don’t have any minus signs. The question then comes up of how you then choose to represent negative values.

One popular way of doing this is to use signed two’s complement form. The way this works is that you write the number using ones and zeros, except that the meaning of those ones and zeros differs from "standard" binary in how they’re interpreted. Specifically, if you have a signed 8-bit number, the lower seven bits have their standard meaning as 20, 21, 22, etc. However, the meaning of the most significant bit is changed: instead of representing 27, it represents the value -27.

So let’s look at the number 0b11111111. This would be interpreted as

-27 + 26 + 25 + 24 + 23 + 22 + 21 + 20

= -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= -1

which is why this collection of bits represents -1.

There’s another way to interpret what’s going on here. Given that our integer only has eight bits to work with, we know that there’s no way to represent all possible integers. If you pick any 257 integer values, given that there are only 256 possible bit patterns, there’s no way to uniquely represent all these numbers.

To address this, we could alternatively say that we’re going to have our integer values represent not the true value of the integer, but the value of that integer modulo 256. All of the values we’ll store will be between 0 and 255, inclusive.

In that case, what is 0 - 1? It’s -1, but if we take that value mod 256 and force it to be nonnegative, then we get back that -1 = 255 (mod 256). And how would you write 255 in binary? It’s 0b11111111.

There’s a ton of other cool stuff to learn here if you’re interested, so I’d recommend reading up on signed and unsigned two’s-complement numbers.

As some exercises: what would -4 look like in this format? How about -9?

These aren't the only ways you can represent numbers in a computer, but they're probably the most popular. Some older computers used the balanced ternary number system (notably the Setun machine). There's also the one's complement format, which isn't super popular these days.

这篇关于8位二进制从0减1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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