bithifting如何在Java中工作? [英] How does bitshifting work in Java?

查看:142
本文介绍了bithifting如何在Java中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的陈述:


假设字节的位值 x 是00101011. x>> 2 的结果是什么?

Assume the bit value of byte x is 00101011. what is the result of x>>2?

我该怎么编程呢,有人可以解释一下我在做什么吗?

How can I program it and can someone explain me what is doing?

推荐答案

首先,你可以在java中移动字节,你只能移动 int long 。所以字节将首先进行促销,例如

Firstly, you can not shift a byte in java, you can only shift an int or a long. So the byte will undergo promotion first, e.g.

00101011 - > 00000000000000000000000000101011

11010100 - > 11111111111111111111111111010100

现在, x>> N 表示(如果您将其视为二进制数字字符串):

Now, x >> N means (if you view it as a string of binary digits):


  • 最右边的N位被丢弃

  • 根据需要复制最左边的位以将结果填充到原始大小(32或64位),例如

00000000000000000000000000101011>> 2 - > 00000000000000000000000000001010

11111111111111111111111111010100>> 2 - > 11111111111111111111111111110101

这篇关于bithifting如何在Java中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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