本文介绍了如何判断十六进制值是否为负?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习了如何读取十六进制值.到目前为止,我只是将它们读为正数.听说您也可以编写负十六进制值.

I have just learned how to read hexadecimal values. Until now, I was only reading them as positive numbers. I heard you could also write negative hex values.

我的问题是我无法确定值是负数还是正值.我在这里和那里找到了一些解释,但是如果我尝试使用在线十六进制到十进制转换器来验证它们,它们总是会给我不同的结果.

My issue is that I can't tell if a value is negative or positive.I found a few explanations here and there but if I try to verify them by using online hex to decimal converters, they always give me different results.

我发现的来源:
https://stackoverflow.com/a/5827491/5016201
https://coderanch.com/t/246080/java-programmer-OCPJP/certification/负十六进制数

如果我正确理解,则表示:
如果写入的所有值的十六进制值的第一个十六进制数字均大于7,则该数字为负.
开头或第一位的所有'F'表示该值是负数,不会被计算.

例如,如果十六进制值用32位写入:
FFFFF63C =>负数(-2500?)
844fc0bb =>否定(-196099909?)
F44fc0bb =>否(-196099909?)
FFFFFFFF =>负数(-1?)
7FFFFFFF =>正

Sources I found:
https://stackoverflow.com/a/5827491/5016201
https://coderanch.com/t/246080/java-programmer-OCPJP/certification/Negative-Hexadecimal-number

If I understand correctly it means that:
If a hex value written with all its bits having something > 7 as its first hex digit, it is negative.
All 'F' at the beginning or the first digit means is that the value is negative, it is not calculated.

For exemple if the hex value is written in 32 bits:
FFFFF63C => negative ( -2500 ?)
844fc0bb => negative ( -196099909 ?)
F44fc0bb => negative ( -196099909 ?)
FFFFFFFF => negative ( -1 ?)
7FFFFFFF => positive

我正确吗?如果没有,您能告诉我我做错了什么吗?

Am I correct? If not, could you tell me what I am not getting right?

推荐答案

阅读Two的补码表示形式: https://en.wikipedia.org/wiki/Two%27s_complement

Read up on Two's complement representation: https://en.wikipedia.org/wiki/Two%27s_complement

我认为了解(通常)如何处理负数的最简单方法是写下一个小的二进制数,然后找出如何进行减法运算.当您达到0并再次应用该方法时,您会发现突然得到全1.这就是"-1"的表示方式(通常):二进制中的所有1或十六进制中的所有f.通常,如果您使用带符号的数字,则它们的第一个(最高有效)位为1来表示.也就是说,如果您使用的位数是四的整数倍,那么如果第一个十六进制数字为8,9,A,B,C,D,E或F,则该数字为负.

I think that the easiest way to understand how negative numbers (usually) are treated is to write down a small binary number and then figure out how to do subtraction by one. When you reach 0 and apply that method once again - you'll see that you suddenly get all 1's. And that is how "-1" is (usually) represented: all ones in binary or all f's in hexadecimal. Commonly, if you work with signed numbers, they are represented by the first (most significant) bit being one. That is to say that if you work with a number of bits that is a multiple of four, then a number is negative if the first hexadecimal digit is 8,9,A,B,C,D,E or F.

求反的方法是:

  1. 反转所有位
  2. 添加1

此表示形式(二进制补码)的另一个好处是,您只会获得一个表示0的表示形式,如果通过设置MSB或将它们取反来标记带符号的数字,则不会是这种情况.

Another benefit from this representation (two's complement) is that you only get one representation for zero, which would not be the case if you marked signed numbers by setting the MSB or just inverting them.

这篇关于如何判断十六进制值是否为负?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 17:35
查看更多