Java 移位运算符 图解_java的移位运算符面向的运算对象

移位运算符面向的运算对象也是二进制的“位”。可单独用它们处理整数类型(主类型的一种)。左移位运算符(<>)则将运算符左边的运算对象向右移动运算符右侧指定的位数。“有符号”右移位运算符使用了“符号扩展”:若值为正,则在高位插入0;若值为负,则在高位插入1。java也添加了一种“无符号”右移位运算符(>>>),它使用了“零扩展”:无论正负,都在高位插入0。这一运算符是C或C++没有的。

43634c115e1b9b7fd4b5cf6dc51100cf.png

若对char,byte或者short进行移位处理,那么在移位进行之前,它们会自动转换成一个int。只有右侧的5个低位才会用到。这样可防止我们在一个int数里移动不切实际的位数。若对一个long值进行处理,最后得到的结果也是long。此时只会用到右侧的6个低位,防止移动超过long值里现成的位数。但在进行“无符号”右移位时,也可能遇到一个问题。若对byte或short值进行右移位运算,得到的可能不是正确的结果(Java 1.0和Java 1.1特别突出)。它们会自动转换成int类型,并进行右移位。但“零扩展”不会发生,所以在那些情况下会得到-1的结果。可用下面这个例子检测自己的实现方案:

//: URShift.java

// Test of unsigned right shift

public class URShift {

public static void main(String[] args) {

int i = -1;

i >>>= 10;

System.out.println(i);

long l = -1;

l >>>= 10;

System.out.println(l);

short s = -1;

s >>>= 10;

System.out.println(s);

byte b = -1;

b >>>= 10;

System.out.println(b);

}

} ///:~

移位可与等号(<<=或>>=或>>>=)组合使用。此时,运算符左边的值会移动由右边的值指定的位数,再将得到的结果赋回左边的值。

下面这个例子向大家阐示了如何应用涉及“按位”操作的所有运算符,以及它们的效果:

//: BitManipulation.java

// Using the bitwise operators

import java.util.*;

public class BitManipulation {

public static void main(String[] args) {

Random rand = new Random();

int i = rand.nextInt();

int j = rand.nextInt();

pBinInt("-1", -1);

pBinInt("+1", +1);

int maxpos = 2147483647;

pBinInt("maxpos", maxpos);

int maxneg = -2147483648;

pBinInt("maxneg", maxneg);

pBinInt("i", i);

pBinInt("~i", ~i);

pBinInt("-i", -i);

pBinInt("j", j);

pBinInt("i & j", i & j);

pBinInt("i | j", i | j);

pBinInt("i ^ j", i ^ j);

pBinInt("i << 5", i << 5);

pBinInt("i >> 5", i >> 5);

pBinInt("(~i) >> 5", (~i) >> 5);

pBinInt("i >>> 5", i >>> 5);

pBinInt("(~i) >>> 5", (~i) >>> 5);

long l = rand.nextLong();

long m = rand.nextLong();

pBinLong("-1L", -1L);

pBinLong("+1L", +1L);

long ll = 9223372036854775807L;

pBinLong("maxpos", ll);

long lln = -9223372036854775808L;

pBinLong("maxneg", lln);

pBinLong("l", l);

pBinLong("~l", ~l);

pBinLong("-l", -l);

pBinLong("m", m);

pBinLong("l & m", l & m);

pBinLong("l | m", l | m);

pBinLong("l ^ m", l ^ m);

pBinLong("l << 5", l << 5);

pBinLong("l >> 5", l >> 5);

pBinLong("(~l) >> 5", (~l) >> 5);

pBinLong("l >>> 5", l >>> 5);

pBinLong("(~l) >>> 5", (~l) >>> 5);

}

static void pBinInt(String s, int i) {

System.out.println(

s + ", int: " + i + ", binary: ");

System.out.print(" ");

for(int j = 31; j >=0; j--)

if(((1 << j) & i) != 0)

System.out.print("1");

else

System.out.print("0");

System.out.println();

}

static void pBinLong(String s, long l) {

System.out.println(

s + ", long: " + l + ", binary: ");

System.out.print(" ");

for(int i = 63; i >=0; i--)

if(((1L << i) & l) != 0)

System.out.print("1");

else

System.out.print("0");

System.out.println();

}

} ///:~

程序末尾调用了两个方法:pBinInt()和pBinLong()。它们分别操作一个int和long值,并用一种二进制格式输出,同时附有简要的说明文字。目前,可暂时忽略它们具体的实现方案。

大家要注意的是System.out.print()的使用,而不是System.out.println()。print()方法不会产生一个新行,以便在同一行里罗列多种信息。

除展示所有按位运算符针对int和long的效果之外,本例也展示了int和long的最小值、最大值、+1和-1值,使大家能体会它们的情况。注意高位代表正负号:0为正,1为负。下面列出int部分的输出:

-1, int: -1, binary:

11111111111111111111111111111111

+1, int: 1, binary:

00000000000000000000000000000001

maxpos, int: 2147483647, binary:

01111111111111111111111111111111

maxneg, int: -2147483648, binary:

10000000000000000000000000000000

i, int: 59081716, binary:

00000011100001011000001111110100

~i, int: -59081717, binary:

11111100011110100111110000001011

-i, int: -59081716, binary:

11111100011110100111110000001100

j, int: 198850956, binary:

00001011110110100011100110001100

i & j, int: 58720644, binary:

00000011100000000000000110000100

i | j, int: 199212028, binary:

00001011110111111011101111111100

i ^ j, int: 140491384, binary:

00001000010111111011101001111000

i << 5, int: 1890614912, binary:

01110000101100000111111010000000

i >> 5, int: 1846303, binary:

00000000000111000010110000011111

(~i) >> 5, int: -1846304, binary:

11111111111000111101001111100000

i >>> 5, int: 1846303, binary:

00000000000111000010110000011111

(~i) >>> 5, int: 132371424, binary:

00000111111000111101001111100000

数字的二进制形式表现为“有符号2的补值”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值