请帮忙,解释一下关于移位的问题,急,不明白

bvk2008 2004-10-17 07:38:28
本人在think in java 中看到移位的一个显示整数的二进制位数的函数请帮忙解释一下。
import com.bruceeckel.simpletest.*;
import java.util.*;

public class BitManipulation {
static Test monitor = new Test();
public static void main(String[] args) {
Random rand = new Random();
int i = rand.nextInt();
int j = rand.nextInt();
printBinaryInt("-1", -1);
printBinaryInt("+1", +1);
int maxpos = 2147483647;
printBinaryInt("maxpos", maxpos);
int maxneg = -2147483648;
printBinaryInt("maxneg", maxneg);
printBinaryInt("i", i);
printBinaryInt("~i", ~i);
printBinaryInt("-i", -i);
printBinaryInt("j", j);
printBinaryInt("i & j", i & j);
printBinaryInt("i | j", i | j);
printBinaryInt("i ^ j", i ^ j);
printBinaryInt("i << 5", i << 5);
printBinaryInt("i >> 5", i >> 5);
printBinaryInt("(~i) >> 5", (~i) >> 5);
printBinaryInt("i >>> 5", i >>> 5);
printBinaryInt("(~i) >>> 5", (~i) >>> 5);

long l = rand.nextLong();
long m = rand.nextLong();
printBinaryLong("-1L", -1L);
printBinaryLong("+1L", +1L);
long ll = 9223372036854775807L;
printBinaryLong("maxpos", ll);
long lln = -9223372036854775808L;
printBinaryLong("maxneg", lln);
printBinaryLong("l", l);
printBinaryLong("~l", ~l);
printBinaryLong("-l", -l);
printBinaryLong("m", m);
printBinaryLong("l & m", l & m);
printBinaryLong("l | m", l | m);
printBinaryLong("l ^ m", l ^ m);
printBinaryLong("l << 5", l << 5);
printBinaryLong("l >> 5", l >> 5);
printBinaryLong("(~l) >> 5", (~l) >> 5);
printBinaryLong("l >>> 5", l >>> 5);
printBinaryLong("(~l) >>> 5", (~l) >>> 5);
monitor.expect("BitManipulation.out");
}
//以下函数不明白:
static void printBinaryInt(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)//重点就是这个判定不明白,j左移1位之后得到的不也是一个整数
//吗?i也是整数,两个整数进行位运算结果可能有0的情况吗?
//更何况这种printBinaryInt("~i", ~i)个函数,如何解释呢,
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
static void printBinaryLong(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();
}
} ///:~
...全文
102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
noproblem12 2004-12-03
  • 打赏
  • 举报
回复
楼主理解错了

1<<j

0000 0000 0000 0000 0000 0000 0000 0001 左移j 位
不是j左移1位
bvk2008 2004-10-21
  • 打赏
  • 举报
回复
i=65=0000 0000 0100 0001
for(int j = 31; j >= 0; j--)
if(((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
当j=31时 j=0000 0000 0001 1111
1 << j的值 (1 << j) & i的值 显示
0000 0000 0011 1110 0000 0000 0011 1110
0000 0000 0100 0001
---------------------------------
0000 0000 0000 0000 对吗? 0
-------------------------------------------------------------
第二次循环,j是变为30,还是62-1=61呢?








--------------------------------------------------
i=59081716
00000011100001011000001111110100
当j=31时 j=0000 0000 0001 1111
1 << j的值 (1 << j) & i的值 显示
0000 0000 0000 0000 0000 0000 0011 1110 \\0000 0000 0000 0000 0000 0000 0011 1110 \
0000 0011 1000 0101 1000 0011 1111 0100
----------------------------------------------
0000 0000 0000 0000 0000 0000 0011 0100
这个结果也不为(!= 0)呀 应该显示“1”,还是应该显示“0”?
wdong18 2004-10-17
  • 打赏
  • 举报
回复
i=0000 0100
当j=3时((1 << j) & i) != 0
其他时候((1 << j) & i) = 0

可以用于逻辑控制。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧