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 ...
随机推荐
- cigarette
#include<iostream> using namespace std; int main() { int N; cin>>N; while(N--) { int Who ...
- Oracle 导入导出 创建用户等
localhost:1158/emD:\app\Administrator\product\11.2.0\dbhome_1\bin\imp.exe log path E:\app\Administ ...
- Cisco路由器交换机配置命令详解
1. 交换机支持的命令: 交换机基本状态:switch: :ROM状态, 路由器是rommon>hostname> :用户模式hostname# :特权模式hostname(config) ...
- 2008R2域控环境中 应用组策略 实现禁用USB设备使用
本文介绍如何在Windows Server 2008 AD中禁用客户端USB端口.本文使用的系统:Windows Server 2008 R2 企业版.域功能级别:Windows Server 200 ...
- Python正则表达式初识(四)
今天继续给大家分享Python正则表达式基础知识,主要给大家介绍一下特殊字符“{}”的用法,具体的教程如下. 特殊字符“{}”实质上也是一个限定词的用法,其限定前面字符所出现的次数,其常用的模式有三种 ...
- SpringMVC与SpringBoot返回静态页面遇到的问题
1.SpringMVC静态页面响应 package com.sv.controller; import org.springframework.stereotype.Controller; impor ...
- GPU-directX的发展历史
GPU发展历史: GPU之前的基础: 1962 麻省理工学院的博士伊凡•苏泽兰发表的论文以及他的画板程序奠定了计算机图形学的基础. 1962-1984 没有专门图形处理硬件,由CPU完成 1984 专 ...
- CodeForcesGym 100502G Outing
Outing Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Origi ...
- [Python] Execute a Python Script
Python scripts can be executed by passing the script name to the python command or created as execut ...
- ORACLE-016:ora-01720 授权选项对于'xxxx'不存在
报错的情形例如以下. A用户:视图V_A B用户:视图V_B,而且用到了V_A C用户:须要用V_B, 授权过程, A用户下: grant select on V_A to B B用户下: grant ...