ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《迪哥Java教程》#5 计算机是怎样保存负数的? – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《迪哥Java教程》#5 计算机是怎样保存负数的?

How do computers store negative numbers? - 005

嘿!这节课我们来学习“negative”(多义词:底片 负数等)
Hey! In this lesson we’re gonna get negative.
不 不是这种negative!(此处为不是底片的意思)
No, not that kind of negative!
我是一个比较较真的人(以工程师的思维来看杯子的尺寸是不对的 表达自己更加极客的意思)
I’m more of a “the glass is incorrectly sized” kinda guy anyway.
我们来讨论Java中如何存储负数
We’re going to talk about negative numbers in how we store negative numbers in Java.
如果不了解计算机存储负数有什么不同(指与正数的区别)
If you’re not sure how computer store negative numbers differently,
你一定要继续收看本视频
you’ll definitely wanna keep watching this video.
这节课的目标是理解如何以二进制形式存储负数
The goals for this lesson are to understand how we store negative numbers in binary.
这也是Java中存储数字的方式
This is also how numbers are stored in Java.
我们会使用一种叫做二进制补码的东西 现在让我从已知的知识开始
We’re gonna use something called two’s complement numbers.Now let’s start with what we know.
上节课 我们讲了二进制数字
We talked about binary numbers in the last lesson,
并且讲了它们如何表示正数
and how they’re organized to represent positive numbers.
每个数字代表2的幂次方
Each digit represents a power of two.
就像10进制数位权是10的幂次方一样
This is like the normal power of 10 numbers we already use.
那么二进制中负数的工作原理是什么呢?
So how does it work with negative numbers?
你首先想到的可能是舍弃第一位的数字 把它作为符号位
Well your first thought might be to sacrifice the first digit, and use it as a sign bit.
让我们试试 先从三位数开始
Let’s try that! Let’s start with three bit numbers
用-3加上2
and add -3 and 2.
第一个数字是111 也就是-3
The first number will be 111. That will be our -3.
然后 用它加上010 也就是2
And then we’ll add 010. That’s our two.
计算结果是001或 1
Add it and we get 001 or one.
等等 -3加2 结果不对了
Wait -3 plus two. That didn’t work!!
计算结果应该是101才对 答案是-1
We should get 101 as a result. The answer is -1.
也许我们需要增加特定代码来处理符号位
Maybe if we add special code for the sign bit?
但我们怎么知道什么时候用程序做这件事?
But how will we know when to do that programmatically?
这种解决策略的问题是需要考虑多种情况
The problem with this strategy is it induces a bunch of cases.
一种是两个正数的情况
A case for two positive numbers.
一种是两个负数的情况 一种是正数大一些
A case for two negative numbers. A case where the positive number is larger.
一种是负数的绝对值大一些
Case where negative number is larger.
一种情况是正负数相加得零
A case where the positive and negative number add to zero.
这完全破坏了加法的简单性
This completely wrecks our simple adder.
不能这样做 好 时间到
It won’t work. Okay time out!
话分两头 先来看看一些其他可能有帮助的东西
Now I’m gonna take an aside. We need to talk about something else that might help
一会回来再看这个问题
and come back to this problem later.
想象钟表的样子 表盘上有1到12 12个数字
Imagine a clock. On the clock you have the numbers one to twelve.
如果在三点钟加上12个小时
If it’s three o’clock and you add 12 hours
仍然是3点钟 我们不关心日期 仅仅关注小时
its still three o’clock. We’re not looking at days here. Just the hour.
如果我们增加三个小时
If we add three hours
变成了6点 加15小时
that makes it six. Add fifteen hours
仍然是6点
and it’s still six.
如果减少9个小时
If we subtract nine hours,
也是6点钟
yep still six.
对乘法也适用 你可以在家试试
This works for multiplication too. You can try that one at home.
能得到正确结果的原因是3 -9
The reason this works is 3, -9,
15对钟表来说是等价的 你用那个都没有关系
and 15 are all equivalent numbers for a clock. It doesn’t matter which one you use.
最终你会得到同样的数字
You get the same number in the end.
用数学方式来描述这种情况它们对12有相同的模
The math way to say this is that they are all congruent modulo 12.
别担心 这不是课堂测验
Don’t worry, that’s not on the quiz.
回到二进制的话题上
So, back to binary land.
怎么把相同的知识用于计算机中的数字呢?
How can we use the same information for our computer numbers.
来对数字做同样的事情
We do the same thing for our numbers.
假设计算机只有3个比特 我们只用三个比特
Imagine our computer holds 3 bits. We’re only using 3 bits because
因为我想向你们展示计算机中每种可能的数字
I want to show you every possible number.
但计算机中 数字的变化范围从8位到64位
On a computer the numbers vary from 8 bits to 64 bits.
那样得话要显示的数字就太多了
That’s just too many to display here.
现在考虑将数字作为钟表上的对应位置
Now think of the numbers as positions on the clock.
我们用以下种方式转换数字
We’ll map our numbers this way.
第一位仍让代表负数
The first bit still means negative,
但其他位却不是你要表示得正数
but the other bits are not how you’d represent positive numbers.
比如-4是100 -1是111
For example -4 is 100 and -1 is 111.
正数看上去一样 负数看起来比较怪
So the positive numbers look the same and negative numbers look weird.
发生了什么?
What happens now?
再来用-3加上2
Let’s try adding -3 and 2 again.
让101与010相加
So we add 101 and 010.
结果是111 也就是-1 结果对了 没有特别的逻辑
That gives us 111 which is -1! It works! No special logic.
计算正确是因为它们是二进制中的等效数
It works because these are equivalent numbers in binary.
那么怎样计算用16位表示的特别的数字呢?
So how do we calculate the special number when we’re using 16 bits?
其实没有什么窍门
Well there’s really no magic to it.
改变数字符号
To change the sign of a number.
所有位取反 再加一
All the bits are flipped and you add one.
刚才我们用的-3
Earlier we had -3.
二进制中的3是011
So three in binary is 011.
第一步 所有位取反得到100
Step 1, flip the bits to get 100.
然后加一
Then we add one.
补码是101
So the complement number is 101.
像在钟表上一样 如果数字是8位的
Just like on our clock. If this number were eight bits
这个数字应该是1111 1101
the number would be 1111 1101.
计算正确等效数与映射关系中的比特位数有关
So the number of bits in your mapping matter to calculate the correct equivalent number.
非常幸运地是所有这些都是在后台处理的
Luckily this is all handled for us behind-the-scenes.
然而理解计算机内部原理仍是很重要的
However it is important to understand what your computer is doing.
以上是为什么在Java中用补码来表示所有数字的原因
So this is why we use two’s complement for all numbers in Java.
Java中没有无符号数字
There are no unsigned numbers in Java.
每种计数数字类型都是这样处理负数的
Every counting number type is doing this for negative numbers.
对一些大数类型 有一些方法可以把他们当作无符号数
For some of the larger number types there are ways to use them unsigned,
在讲到Number类时再来介绍
but we can’t cover that until we get to number classes.
那是另一种方式 下节课我们才会看到第一种数据类型
That’s a ways away. In the next lesson we’ll actually look at out first type.
字节类型
The byte.
嘿 感谢收看本视频 如果你想检查学习效果
Hey thanks for watching the video! There is a quick quiz for this on DeegeU.com
DeegeU.com官网上有一个快速测试
if you’d like to gauge how much you learned.
如果你喜欢这个视频系列 顶这个视频然后点击订阅Youtube上的DeegeU频道
If you like the videos you are seeing please let me know by liking the video and
来让我知道
hitting the subscribe button for the DeegeU channel on YouTube. I’d
非常感谢 如果你有任何疑惑问题 请到
really appreciate that! If you have concerns or questions please leave them
在下面或DeegeU.com的评论区留言
in the comments below or on DeegeU.com.
DeegeU.com首页上有一个投票
There’s a poll on the front page of DeegeU.com,
你们可以告诉我下节课想了解的主题
so you can let me know what topic is covered next.
感谢收看 下次再见
Thanks for watching and see you in the next video!

发表评论

译制信息
视频概述

讲述计算机中如何存储负数 讲述了什么是补码 补码和位数的关系

听录译者

收集自网络

翻译译者

谷子

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=Yp-hfi3O9Yc

相关推荐