1. Java provides operators to perform bitwise and bit shift operations on int type.
The operators discussed in the section are less commonly used, the intent is to simply make you aware that these operators exist.
2. Bitwise operators overview:
a & b |
and | 3 & 5 | 1 | 1 if both bits are 1. |
a | b |
or | 3 | 5 | 7 | 1 if either bit is 1. |
a ^ b |
xor | 3 ^ 5 | 6 | 1 if both bits are different. |
~a |
not | ~3 | -4 | Inverts the bits. |
n << p |
left shift | 3 <<< 2 | 12 | Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions. |
n >> p |
right shift | 5 >> 2 | 1 | Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions. |
n >>> p |
right shift | -4 >>> 28 | 15 | Shifts the bits of n right p positions. Zeros are shifted into the high-order positions. |
3. Examples
1) "AND" example:
@Test public void andTest() { int i = 12; // i = 0000,0000,0000,0000,0000,0000,0000,1100 int j = -3; // -3 = ~2 -> ~0000,0000,0000,0000,0000,0000,0000,0010 -> 1111,1111,1111,1111,1111,1111,1111,1101 int k = i & j; // 0000,0000,0000,0000,0000,0000,0000,1100 & // 1111,1111,1111,1111,1111,1111,1111,1101 -> // 0000,0000,0000,0000,0000,0000,0000,1100 ->12 assertEquals(12, k); }
2) "OR" example:
@Test public void orTest() { int i = 12; // i = 0000,0000,0000,0000,0000,0000,0000,1100 int j = -32768; // -32768 = ~32767 -> ~0000,0000,0000,0000,0111,1111,1111,1111 -> // 1111,1111,1111,1111,1000,0000,0000,0000 int k = i | j; // 0000,0000,0000,0000,0000,0000,0000,1100 | // 1111,1111,1111,1111,1000,0000,0000,0000 -> // 1111,1111,1111,1111,1000,0000,0000,1100 -> // -(~1111,1111,1111,1111,1000,0000,0000,1100 + 1) -> // -( 0000,0000,0000,0000,0111,1111,1111,0100) -> -32756 assertEquals(-32756, k); }
3) "XOR" example:
@Test public void xorTest() { int i = 32756; // i = 0000,0000,0000,0000,0111,1111,1111,0100 int j = -32768; // -32768 = ~32767 -> ~0000,0000,0000,0000,0111,1111,1111,1111 -> // 1111,1111,1111,1111,1000,0000,0000,0000 int k = i ^ j; // 0000,0000,0000,0000,0111,1111,1111,0100 ^ // 1111,1111,1111,1111,1000,0000,0000,0000 -> // 1111,1111,1111,1111,1111,1111,1111,0100 -> // -(~1111,1111,1111,1111,1111,1111,1111,0100 + 1) -> // -( 0000,0000,0000,0000,0000,0000,0000,1100) -> -12 assertEquals(-12, k); }
4) "NOT" test
@Test public void notTest() { int i = -12; // ~11 -> // ~0000,0000,0000,0000,0000,0000,0000,1011 -> // 1111,1111,1111,1111,1111,1111,1111,0100 int j = ~i; // ~1111,1111,1111,1111,1111,1111,1111,0100 // 0000,0000,0000,0000,0000,0000,0000,1011 assertEquals(11, j); i = -32768; j = ~i; assertEquals(32767, j); i = 0XFFFFFFFF; j = ~i; assertEquals(0, j); }
5) "Left Shift" test:
@Test public void leftShiftTest() { int i = 12; // 0000,0000,0000,0000,0000,0000,0000,1100 int j = i << 1; // 0000,0000,0000,0000,0000,0000,0001,1000 assertEquals(24, j); i = 0X8000000F; // 1000,0000,0000,0000,0000,0000,0000,1111 j = i << 1; // 0000,0000,0000,0000,0000,0000,0001,1110 assertEquals(30, j); }
6) "Right Shift" test:
@Test public void rightShiftTest() { int i, j; i = 12; // 0000,0000,0000,0000,0000,0000,0000,1100 j = i >> 1; // 0000,0000,0000,0000,0000,0000,0000,0110 assertEquals(6, j); j = i >>> 1; // 0000,0000,0000,0000,0000,0000,0000,0110 assertEquals(6, j); i = 0X8000000F; // 1000,0000,0000,0000,0000,0000,0000,1111 j = i >> 1; // 1100,0000,0000,0000,0000,0000,0000,0111 assertEquals(0XC0000007, j); j = i >>> 1; // 0100,0000,0000,0000,0000,0000,0000,0111 assertEquals(0X40000007, j); }
4. P.S
1) The size of "int" in java is 32-bit that is 8-byte, thus we can represent that with 8 hex number.
2) The left most bit represents the sign, 0 for positive and 1 for negative.
3) There is no unsigned int/byte data type in java, and we can use "long" if needed.
Reference Links:
1) http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
2) http://www.leepoint.net/notes-java/data/expressions/bitops.html
3) http://stackoverflow.com/questions/9854166/declaring-an-unsigned-int-in-java
相关推荐
按位与:bitwise_and(); 按位或:bitwise_or(); 按位异或:bitwise_xor()。 按位反转:bitwise_not(); 【注】以上所有运算皆基于二进制而来。 举例说明 1、bitwise_and() a = np.array([[1], [9], [1], ...
OpenCV-Python图像位与运算bitwise_and函数详解.rar
自动化BitWise设备远程控制自动化库
3.cv.bitwise_and或者cv.bitwise_or import cv2 as cv import numpy as np import os.path p1=../image/linux.png\np2='../image/win.png' #中文转换编码 def zh_ch(string): return string.encode(gbk).decode...
matlab椒盐去噪代码按位CA操作 2017年6月发布的《灰度图像上的细胞自动机的按位运算》代码库。 所有代码均独立于MATLAB 2016中的第三人称库编写,并在Python27中进行了绘图和数据处理。 特定文件夹具有相应的readme....
Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops -...
level5-04-bitwise-operations-jaLeague:level5-04-bitwise-operations-jaLeague由GitHub Classroom创建
Volume 4, Fascicle 1 This fascicle, enlivened by a wealth of Knuth's typically enjoyable examples, describes basic "broadword" operations and an important class of data structures that can make ...
level5-04-bitwise-operations-eliyahubowers:GitHub Classroom创建的level5-04-bitwise-operations-eliyahubowers
按位运算 启用按位操作的运算符的说明性示例。 该程序用C ++编写,以便std::bitset STL std::bitset轻松显示位。 安装 不包括makefile,只需使用您最喜欢的C ++编译器进行编译并...Use bitwise AND & to switch off byt
bitwise_operations_cpp 按位运算符 操作员 意义 描述 和 按位与 对两个数字的每一位执行与运算。 仅当两个位均为1时才为1 | 按位或 对两个数字的每一位进行“或”运算。 如果两个位中的任何一位为1,则为1。 ^ ...
按位 JavaScript / TypeScript库可操作位,半字节,字节和... and ( [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 0 , 1 ] ) // [0, 0, 0, 1] bitwise . bits . xor ( [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 0 , 1 ] ) // [0, 1, 1, 0] //
Programming Interview Problems and Algorithms in Ruby by Zachary Paul English | April 17, 2016 | ASIN: B01EGILLLS | 177 Pages ... Bitwise Operations, Dynamic Programming, and many others.
A test harness that checks a student's solution in bits.c
The decoding algorithm used in RBDS.c is based on error... The program emulates the operation of the encoder and decoder of a binary cyclic codes, using bitwise shifts and xor for modulo g(x) operations.
Chapter 11: The C Preprocessor and Bitwise Operations Chapter 12: Arduino Libraries Chapter 13: Interfacing to the Outside World Chapter 14: A Gentle Introduction to Object-Oriented Programming and ...
Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops -...
Bitwise是一个教育项目,能让我们从头开始为计算机创建软件/硬件堆栈
人类位向量:trade_mark: 这个简单的位向量实现旨在使寻址单个位不那么繁琐。...> power = BitField ( 0 , 1 ) # offset and size > spin = BitField ( 1 , 1 ) > speed = BitField ( 2 , 4 ) > sense = BitField