java右划线转移_按位运算符-Java中的移位如何工作?

按位运算符-Java中的移位如何工作?

我有这句话:

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

我该如何编程,有人可以解释我在做什么?

10个解决方案

94 votes

首先,您不能在Java中转移00101011,而只能转移int或long。因此byte将首先接受晋升,例如

00101011-> 11111111111111111111111111010100 >> 2

要么

11111111111111111111111111010100 >> 2-> 11111111111111111111111111110101

现在,11111111111111111111111111010100 >> 2表示(如果您将其视为一串二进制数字):

最右边的N位被丢弃

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

11111111111111111111111111010100 >> 2-> 11111111111111111111111111110101

11111111111111111111111111010100 >> 2-> 11111111111111111111111111110101

finnw answered 2019-10-25T00:00:40Z

57 votes

82125c453e6eddd079ca80cb45ce1a04.png

00000000 00000000 00000000 00101011的二进制32位为

00000000 00000000 00000000 00101011,结果是:

00000000 00000000 00000000 00101011 >> 2(times)

\\ \\

00000000 00000000 00000000 00001010

将43位右移距离2; 用左侧的最高(符号)位填充。

结果为00001010,十进制值为10。

00001010

8+2 = 10

Chandra Sekhar answered 2019-10-25T00:01:24Z

16 votes

当您右移2位时,将丢弃2个最低有效位。 所以:

x = 00101011

x >> 2

// now (notice the 2 new 0's on the left of the byte)

x = 00001010

这与将整数除以2、2倍本质上是相同的。

在Java中

byte b = (byte) 16;

b = b >> 2;

// prints 4

System.out.println(b);

jjnguy answered 2019-10-25T00:02:02Z

10 votes

这些示例涵盖了应用于正数和负数的三种类型的移位:

// Signed left shift on 626348975

00100101010101010101001110101111 is 626348975

01001010101010101010011101011110 is 1252697950 after << 1

10010101010101010100111010111100 is -1789571396 after << 2

00101010101010101001110101111000 is 715824504 after << 3

// Signed left shift on -552270512

11011111000101010000010101010000 is -552270512

10111110001010100000101010100000 is -1104541024 after << 1

01111100010101000001010101000000 is 2085885248 after << 2

11111000101010000010101010000000 is -123196800 after << 3

// Signed right shift on 626348975

00100101010101010101001110101111 is 626348975

00010010101010101010100111010111 is 313174487 after >> 1

00001001010101010101010011101011 is 156587243 after >> 2

00000100101010101010101001110101 is 78293621 after >> 3

// Signed right shift on -552270512

11011111000101010000010101010000 is -552270512

11101111100010101000001010101000 is -276135256 after >> 1

11110111110001010100000101010100 is -138067628 after >> 2

11111011111000101010000010101010 is -69033814 after >> 3

// Unsigned right shift on 626348975

00100101010101010101001110101111 is 626348975

00010010101010101010100111010111 is 313174487 after >>> 1

00001001010101010101010011101011 is 156587243 after >>> 2

00000100101010101010101001110101 is 78293621 after >>> 3

// Unsigned right shift on -552270512

11011111000101010000010101010000 is -552270512

01101111100010101000001010101000 is 1871348392 after >>> 1

00110111110001010100000101010100 is 935674196 after >>> 2

00011011111000101010000010101010 is 467837098 after >>> 3

Percy Vega answered 2019-10-25T00:02:28Z

5 votes

>>是算术右移运算符。 第一个操作数中的所有位都移位了第二个操作数指示的位数。 结果中最左边的位设置为与原始编号最左边的位相同的值。 (这样负数就保持负数。)

这是您的具体情况:

00101011

001010

00001010

Mike Daniels answered 2019-10-25T00:03:00Z

4 votes

public class Shift {

public static void main(String[] args) {

Byte b = Byte.parseByte("00101011",2);

System.out.println(b);

byte val = b.byteValue();

Byte shifted = new Byte((byte) (val >> 2));

System.out.println(shifted);

// often overloked are the methods of Integer

int i = Integer.parseInt("00101011",2);

System.out.println( Integer.toBinaryString(i));

i >>= 2;

System.out.println( Integer.toBinaryString(i));

}

}

输出:

43

10

101011

1010

stacker answered 2019-10-25T00:03:21Z

2 votes

byte x = 51; //00101011

byte y = (byte) (x >> 2); //00001010 aka Base(10) 10

Jacob Tomaw answered 2019-10-25T00:03:40Z

2 votes

您不能用Java编写像x >> 2这样的二进制文字,因此可以用十六进制来编写:

byte x = 0x2b;

要计算x >> 2的结果,您可以准确地写出并打印结果。

System.out.println(x >> 2);

Mark Byers answered 2019-10-25T00:04:12Z

2 votes

您可以使用例如 如果您想查看数字的bitString表示,请使用此API。 罕见的数学

示例(在jruby中)

bitString = org.uncommons.maths.binary.BitString.new(java.math.BigInteger.new("12").toString(2))

bitString.setBit(1, true)

bitString.toNumber => 14

编辑:更改了api链接并添加了一个小示例

Joni answered 2019-10-25T00:04:51Z

0 votes

00101011 =十进制43

class test {

public static void main(String[] args){

int a= 43;

String b= Integer.toBinaryString(a >> 2);

System.out.println(b);

}

}

输出:

101011变成1010

Gihan Fernando answered 2019-10-25T00:05:24Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值