Java移位运算符 [英] Java shift operator

查看:92
本文介绍了Java移位运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下Java代码:

Consider the following Java code:

byte a = -64; 
System.out.println(a << 1);

此代码的输出为-128

我尝试如下以弄清为什么是输出:

I tried as follows to figure out why this is the output:

64 = 0 1000000(MSB是符号位)

64 = 0 1000000 (the MSB is the sign bit)

-64 = 1 1000000(Tow的补码格式)

-64= 1 1000000 (Tow's complement format)

移位后的预期输出: 1 0000000(等于0,因为MSB只是一个符号位)

Expected output after shifting: 1 0000000 (This is equal to 0, because the MSB is just a sign bit)

请任何人解释我所缺少的.

Please anyone explain what I am missing.

推荐答案

-128的二进制补码表示为10000000,因此您的结果是正确的.

The two's complement representation of -128 is 10000000, thus your results are correct.

这篇关于Java移位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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