将字节提升为int或long [英] Promotion of byte to int or long

查看:107
本文介绍了将字节提升为int或long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个字节值 00101011 ,我想将其表示为int值,我有: 00000000000000000000000000101011 ,对我来说似乎很清楚。我有字节值 11010100 的问题,其中int由 11111111111111111111111111010100 表示。我不知道它来自哪里。有人可以向我解释扩展字节值到int的想法吗?

If I have an byte value 00101011 and I want to represent it as int value I have: 00000000000000000000000000101011, what seems to be clear for me. Bu I have problem with byte value 11010100 which as int is represented by 11111111111111111111111111010100. I don't know where it came from. Can someone explain me the idea of extension byte value to int?

推荐答案

字节 value 11010100 表示负数( -44 ),因为设置了最高有效位。当这经历原始加宽转换时,它仍然必须在二进制补码表示中表示相同的负值。这是使用符号扩展完成的。这意味着所有新位都与原始符号位相同。

The byte value 11010100 represents a negative number (-44), because the most significant bit is set. When this undergoes primitive widening conversion, it must still represent the same negative value in the two's complement representation. This done using sign extension. That means that all new bits are the same as the original sign bit.

                           11010100 => -44
11111111 11111111 11111111 11010100 => -44

如果没有发生这种情况,则符号位将不再是符号位,并且它将被解释为正常,并且该值将不再相同。

If this did not occur, then the sign bit would no longer be a sign bit, and it would be interpreted "normally", and the value would no longer be the same.

00000000 00000000 00000000 11010100 => 212

这篇关于将字节提升为int或long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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