首页 > 编程语言 > 详细

Java位运算

时间:2020-03-24 14:57:29      阅读:41      评论:0      收藏:0      [点我收藏+]

(很早的笔记,保存下)

整数位运算:

  • 左移:正数右端补0,负数右端补1
  • 右移:正数左端补0,负数左端补1
  • 强制右移:不论正负数左端补0
  • 取反
int a = 5;
int b = -5;
int c = (~5)+1;
int d = a<<1;
int e = b<<1;
int f = a>>1;
int g = b>>1;
int h = a>>>1;
int i = b>>>1;

System.out.println("a:"+Integer.toBinaryString(a));
System.out.println("b:"+Integer.toBinaryString(b));
System.out.println("c:"+Integer.toBinaryString(c));
System.out.println("d:"+Integer.toBinaryString(d));
System.out.println("e:"+Integer.toBinaryString(e));
System.out.println("f:"+Integer.toBinaryString(f));
System.out.println("g:"+Integer.toBinaryString(g));
System.out.println("h:"+Integer.toBinaryString(h));
System.out.println("i:"+Integer.toBinaryString(i));
//-------结果------------------------
a:101
b:11111111111111111111111111111011
c:11111111111111111111111111111011
d:1010
e:11111111111111111111111111110110
f:10
g:11111111111111111111111111111101
h:10
i:1111111111111111111111111111101			//注意这里少一位

Java位运算

原文:https://www.cnblogs.com/cdbb/p/12558204.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!