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
相关推荐
### 1Z0-811 Exam Guide:Oracle Java SE Certification Key Knowledge Points #### Introduction to Oracle Java SE Certification 1Z0-811 Examination The 1Z0-811 examination is part of the Oracle Java SE ...
位与运算(bitwise_and)是其中的一个核心函数,常用于图像掩模、合并图像以及创建复杂的选择区域等场景。下面我们将详细讨论OpenCV-Python中的bitwise_and函数及其应用。 位运算通常涉及到二进制表示,它们在...
在编程语言C中,"Pointers and Arrays Structures, Unions and Bit-Fields1"这一主题涵盖了几个核心概念,这些概念对于理解和编写高效的C程序至关重要。首先,我们要理解数据类型(Data Types)、运算符和表达式的...
自动化BitWise设备远程控制自动化库
问题汇总:遇到的报错有: Unsupported ONNX opset version: 17 Exporting the operator repeat_interleave to ONNX opset version 11 is not supported TypeError: 'torch._C.Value' object is not iterable ...
java java_leetcode题解之Bitwise ORs of Subarrays.java
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...
例如,将 short 类型转换成 byte 数组可以使用 bitwise right shift 运算符 (>>) 和 bitwise AND 运算符 (&)。将 int 类型转换成 byte 数组可以使用 bitwise right shift 运算符 (>>) 和 bitwise AND 运算符 (&) 等...
- **Bitwise Operators**: Performing bitwise operations on binary data. - **Ternary Operator**: A shorthand for if-else statements. #### 5. Flow Control, Exceptions, and Assertions This chapter ...
matlab椒盐去噪代码按位CA操作 2017年6月发布的《灰度图像上的细胞自动机的按位运算》代码库。 所有代码均独立于MATLAB 2016中的第三人称库编写,并在Python27中进行了绘图和数据处理。 特定文件夹具有相应的readme....
cv::bitwise_and(src, dilated, skeleton); ``` 这段代码首先创建了一个十字形的结构元素,然后对原始图像进行腐蚀和膨胀。接着,通过将原图与膨胀后的图像进行位运算,我们得到骨架。这个过程需要迭代多次,每次...
《从零构建:Bitwise——探索C++与计算机硬件的深度结合》 Bitwise,一个教育项目,旨在引领我们深入理解计算机的软件和硬件堆栈,从基础开始,逐步构建起一套完整的系统。这个项目特别关注C++语言,因为C++在底层...
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 -...
The section on low-level algorithms covers a wide range of topics related to bit manipulation and word-level operations. Here are some of the key insights and algorithms discussed: 1. **Bit Wizardry*...
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 ...
按位运算 启用按位操作的运算符的说明性示例。 该程序用C ++编写,以便std::bitset STL std::bitset轻松显示位。 安装 不包括makefile,只需使用您最喜欢的C ++编译器进行编译并...Use bitwise AND & to switch off byt
bitwise_operations_cpp 按位运算符 操作员 意义 描述 和 按位与 对两个数字的每一位执行与运算。 仅当两个位均为1时才为1 | 按位或 对两个数字的每一位进行“或”运算。 如果两个位中的任何一位为1,则为1。 ^ ...
然后,可以调用`cv::cvtColor`函数将图像从原始色彩空间转换到灰度色彩空间,或者使用`cv::bitwise_not`函数直接对像素值进行取反操作。 下面是一个简单的VC++代码示例,展示了如何对一幅图像进行反色处理: ```...
本项目“level5-04-bitwise-operations-jaLeague”是GitHub Classroom创建的一个学习资源,旨在帮助Java开发者掌握位运算操作。在Java中,位运算是一种直接对二进制位进行操作的算术和逻辑运算,它们对于理解计算机...