java 位操作 bitwise(按位) operation bit
java 位操作 bitwise(按位) operation bit
//一篇对于 原码 反码 补码 的介绍
http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
java中有三种移位运算符
<< : 左移运算符,num << 1,相当于num乘以2
>> : 右移运算符,num >> 1,相当于num除以2
>>> : 无符号右移,忽略符号位,空位都以0补齐
// 8 0000 0000 0000 1000 原码
1111 1111 1111 0111 反码
+ 1
1111 1111 1111 1000 (8的补码)来表示 -8
// -8 1111 1111 1111 1000 65528 补码(正值 的反码+1)
// 65535 1111 1111 1111 1111 65535
// 65535-65528=7+1=8
操作longValue = longValue & ~(1 << n); 可以在longValue的2进制表示中,把从右边数到左边数第n + 1位设置为0,并且不影响其他位上面的值 即用1和2进制值变量x做与&and操作,不会影响到2进制变量x的值
操作System.out.println((longValue >> n & 1) == 1); 可以判断值longValue的2进制表示中,从右边数到左边第n + 1位的值是0false 还是1true
- public class bitOperation {
- /**
- * @param args
- */
- public static void main(String[] args) {
- long longValue = 0;
- longValue = longValue | (1 << 0);
- // 1
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 1);
- // 11
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 4);
- // 10011
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 5);
- // 110011
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 6);
- // 1110011
- System.out.println(Long.toBinaryString(longValue));
- String hex = Long.toBinaryString(longValue);
- // 1110011
- System.out.println(hex);
- // 115
- System.out.println(Integer.valueOf("1110011", 2));
- // 1110011
- System.out.println(Long.toBinaryString(longValue >> 0));
- // 1
- System.out.println(Long.toBinaryString(longValue >> 0 & 1));
- // 111001
- System.out.println(Long.toBinaryString(longValue >> 1));
- // 1
- System.out.println(Long.toBinaryString(longValue >> 1 & 1));
- // true
- System.out.println((longValue >> 0 & 1) == 1);
- // true
- System.out.println((longValue >> 1 & 1) == 1);
- // false
- System.out.println((longValue >> 2 & 1) == 1);
- // false
- System.out.println((longValue >> 3 & 1) == 1);
- // true
- System.out.println((longValue >> 4 & 1) == 1);
- // true
- System.out.println((longValue >> 5 & 1) == 1);
- // true
- System.out.println((longValue >> 6 & 1) == 1);
- // false
- System.out.println((longValue >> 7 & 1) == 1);
- // Demonstrate the bitwise logical operators.
- bitLogic();
- // Left shifting a byte value.
- byteShift();
- }
- /**
- * Left shifting a byte value.
- */
- private static void byteShift() {
- byte a = 64, b;
- int i;
- i = a << 2;
- b = (byte) (a << 2);
- // Original value of a: 64
- System.out.println("Original value of a: " + a);
- // i and b: 256 0
- System.out.println("i and b: " + i + " " + b);
- System.out.println("\r\n");
- }
- /**
- * Demonstrate the bitwise logical operators.
- */
- private static void bitLogic() {
- String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101",
- "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
- "1110", "1111"
- };
- int a = 3; // 0 + 2 + 1 or 0011 in binary
- int b = 6; // 4 + 2 + 0 or 0110 in binary
- int c = a | b;
- int d = a & b;
- int e = a ^ b;
- int f = (~a & b) | (a & ~b);
- int g = ~a & 0x0f;
- // a = 0011 = 3
- System.out.println(" a = " + binary[a] + " = " + a);
- // b = 0110 = 6
- System.out.println(" b = " + binary[b] + " = " + b);
- // a|b = 0111 = 7
- System.out.println(" a|b = " + binary[c] + " = " + c);
- // a&b = 0010 = 2
- System.out.println(" a&b = " + binary[d] + " = " + d);
- // a^b = 0101 = 5
- System.out.println(" a^b = " + binary[e] + " = " + e);
- // ~a&b|a&~b = 0101 = 5
- System.out.println("~a&b|a&~b = " + binary[f] + " = " + f);
- // ~a = 1100 = 12
- System.out.println(" ~a = " + binary[g] + " = " + g);
- System.out.println("\r\n");
- }
- }
java 位操作 bitwise(按位) operation bit的更多相关文章
- Java位操作全面总结
转载: Java位操作全面总结 在计算机中所有数据都是以二进制的形式储存的.位运算其实就是直接对在内存中的二进制数据进行操作,因此处理数据的速度非常快.在实际编程中,如果能巧妙运用位操作,完全可以达到 ...
- JAVA 位操作学习
一,基础知识 计算机中数值的编码方式中,原码.反码.补码. 正数的补码与原码相同,负数的补码为:负数的原码符号位不变,其它位取反,再加1. 在计算机中,数值是以补码的形式存储的.补码的好处: ①用补码 ...
- Java位操作全面总结[ZZ]
Java位操作全面总结 在计算机中所有数据都是以二进制的形式储存的.位运算其实就是直接对在内存中的二进制数据进行操作,因此处理数据的速度非常快.在实际编程中,如果能巧妙运用位操作,完全可以达到四两拨千 ...
- Java float保留两位小数或多位小数
Java float保留两位小数或多位小数 方法1:用Math.round计算,这里返回的数字格式的. float price=89.89;int itemNum=3;float totalPr ...
- 转:java开发的10位牛人
文章来自于:http://it.deepinmind.com/java/2014/04/10/top-10-java-people-you-should-know.html James Gosling ...
- Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种
Java随机获取32位密码且必须包含大小写字母.数字和特殊字符,四种的任意三种 Java随机获取32位密码且必须包含大小写字母.数字和特殊字符,四种的任意三种,代码如下: import java.ut ...
- 你知道吗?Java开发的10位牛人
James Gosling 1983年,Gosling获得了加州大学的计算机科学学士学位.1990年,他获得了卡内基梅隆大学的计算机科学博士学位,师从Bob Sproull.在攻读博士期间,他自己开发 ...
- Java生成前三位是字母循环的字典
title: Java生成前三位是字母循环的字典 date: 2018-08-17 18:52:22 tags: Java --- 最近要破解一个秘密,还好这个密码是有线索的,已知密码的前三位是三个字 ...
- java 随机生成4位随机数
java 随机生成4位的随机数测试类 @org.junit.Testpublic void testRandom(){ String msg="您的注册码为%s,谢谢注册!"; S ...
随机推荐
- 【AHOI2013】【BZOJ3238】差异
Description Input 一行,一个字符串S Output 一行.一个整数.表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...
- python之文件操作-复制、剪切、删除等
以下是把sourceDir目录下的以.JPG结尾的文件所有拷贝到targetDir目录下: <span style="font-size:18px;">>> ...
- SQLServer2008 R2安装步骤
1.解压缩sqlserver_2008_r2.iso到指定的目录,记住这个目录的位置 sqlserver_2008_r2.iso下载位置是:http://download.csdn.net/u0123 ...
- vue11 vue实例方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- poj--2007--Scrambled Polygon(数学几何基础)
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u Su ...
- noip 2018 day2 T1 旅行 基环树 tarjan
Code: #include<cstdio> #include<cstring> #include<string> #include<stack> #i ...
- blkid---对系统块设备信息查询
在Linux下可以使用blkid命令对查询设备上所采用文件系统类型进行查询.blkid主要用来对系统的块设备(包括交换分区)所使用的文件系统类型.LABEL.UUID等信息进行查询.要使用这个命令必须 ...
- ios程序启动过程和UIWidnow介绍
一.iOS程序的完整启动过程(有storyboard) 1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicationMain函数里面做了什么事情: ...
- Mybatis使用注解进行增删改查
// 增public interface StudentMapper{ @Insert("insert into student (stud_id, name, email, addr_id ...
- [React] Validate Custom React Component Props with PropTypes
In this lesson we'll learn about how you can use the prop-types module to validate a custom React co ...