`

Java位运算符(JAVA Bitwise Logical Operators)

阅读更多
Bitwise Logical Operators(位运算符)由于在一般的日常开发当中很少涉及,所以在《Thinking in java》,《Core Java 2》等Java书籍中只是略有提及,一笔带过。
也没找到一本参考书对其有详细描述,兴趣所致,在网上搜索了许多资料。终于大致了解了其原理。

位运算符包括:~,|,&,^

~ the NOT Operator (非运算符)

| the OR Operator  (或运算符)

& the AND Operator (与运算符)

^ the XOR Operator (异或运算符)





~ the NOT Operator(非运算符)

以下是《Thinking in java》中的描述:
The bitwise NOT (~, also called the ones complement operator) is a unary operator; it takes only one argument. (All other bitwise operators are binary operators.)
非运算符是一个一元运算符,它只需要一个参数。(其它的位运算符都是二元运算符)
Bitwise NOT produces the opposite of the input bit—a one if the input bit is zero, a zero if the input bit is one.
非运算符会对输入(input)进行位取反运算,0变成1,1变成0。(假设输入(input)是一个十进制数,则先取其二进制表示,再进行位取反运算)


示例1
class TheNotOperator {

	public static void main(String args[]) {
		// invert 0
		System.out.println("~ 0 = " + ~0);

		// invert 11
		System.out.println("~ 11 = " + ~11);

	}

}


为何 ~0 = -1?
0用二进制表示为:00000000000000000000000000000000(更多关于二进制与十进制相互转换的信息,见http://denverj.iteye.com/blog/736637
取反就是:       11111111111111111111111111111111
可以看出这是一个负数,因为在二进制中,最左位为1表示负数。要找出它的实际值,我们采用二进制补码算法。(更多二进制补码的信息,见http://denverj.iteye.com/blog/736583
第一步,每一个二进制位都取相反值,0变成1,1变成0。比如,11111111111111111111111111111111的相反值就是00000000000000000000000000000000。
第二步,将上一步得到的值加1。00000000000000000000000000000000就变成00000000000000000000000000000001。
00000000000000000000000000000001代表1。
所以11111111111111111111111111111111代表二进制的-1。

为何 ~11 = -12?
11用二进制表示为:00000000000000000000000000001011
取反就是:        11111111111111111111111111110100
可以看出这是一个负数,因为在二进制中,最左位为1表示负数。要找出它的实际值,我们采用二进制补码算法。
第一步,每一个二进制位都取相反值,0变成1,1变成0。比如,11111111111111111111111111110100的相反值就是00000000000000000000000000001011。
第二步,将上一步得到的值加1。00000000000000000000000000001011就变成00000000000000000000000000001100。
00000000000000000000000000001100代表12。(更多关于二进制与十进制相互转换的信息,见http://denverj.iteye.com/blog/736637
所以11111111111111111111111111110100代表二进制的-12。



| the OR Operator  (或运算符)

以下是《Thinking in java》中的描述:
The bitwise OR operator (|) produces a one in the output bit if either input bit is a one and produces a zero only if both input bits are zero.
如果输入(input)的两位中只要有一个为1,则或运算符会返回1。只有两个都是0,它才返回0。(假设输入(input)是二个十进制数,则先取其二进制表示,再进行具体运算)

示例2
public class TheOrOperator {

	public static void main(String args[]) {

		// apply the | operator
		int x = 11 | 10;

		System.out.println("11|10 = " + x);

	}
}


为何 11|10 = 11?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行非运算符对每一位进行运算,得到00000000000000000000000000001011
00000000000000000000000000001011表示11。



& the AND Operator (与运算符)

以下是《Thinking in java》中的描述:
The bitwise AND operator (&) produces a one in the output bit if both input bits are one, otherwise it produces a zero.
如果输入(input)位都是1,则与运算符返回1。否则为0。(假设输入(input)是二个十进制数,则先取其二进制表示,再进行具体运算)

示例3
public class TheAndOperator {
	public static void main(String args[]) {

		// apply the & operator
		int x = 11 & 10;

		System.out.println("11&10 = " + x);

	}
}


为何 11&10 = 10?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行非运算符对每一位进行运算,得到00000000000000000000000000001010
00000000000000000000000000001010表示10。

^ the XOR Operator (异或运算符)

以下是《Thinking in java》中的描述:
The bitwise EXCLUSIVE OR, or XOR (^), produces a one in the output bit if one or the other input bit is a one, but not both.
如果输入(input)的两位当中有且只有一个为1,则异或运算符会返回1。

示例4
public class TheXOROperator {
	public static void main(String args[]) {
		
		// apply the ^ operator
		int x = 11 ^ 10;

		System.out.println("11^10 = " + x);

	}
}


为何 11^10 = 1?
11用二进制表示为:00000000000000000000000000001011
10用二进制表示为:00000000000000000000000000001010
执行异或运算符对每一位进行运算,得到00000000000000000000000000000001
00000000000000000000000000000001表示1。

参考资料链接:http://blog.csdn.net/yz394777014/archive/2009/07/28/4387728.aspx
                           <Thinking in Java 3rd Edtion>  Bruce Eckel
2
0
分享到:
评论

相关推荐

    详细介绍Python语言中的按位运算符

    按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下: 按位与 ( bitwise and of x and y )  & 举例: 5&3 = 1 解释: 101 11 相同位仅为个位1 ,故结果为 1 按位或 ( bitwise or of x and...

    JAVA基础之java的移位运算

    // Demonstrate the bitwise logical operators. class BitLogic { public static void main(String args[]) { String binary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", ...

    OpenCV-Python图像位与运算bitwise_and函数详解.rar

    OpenCV-Python图像位与运算bitwise_and函数详解.rar

    Laravel开发-laravel-bitwise-trait

    Laravel开发-laravel-bitwise-trait 在任何类上使用位运算符的简单特性

    Bitwise-logical-addition:按位逻辑加法

    按位逻辑加法 该程序应用了按位逻辑运算来实现基本的加法和减法。 完全不使用算术方法。 该程序是为用作ALU(算术逻辑单元)模拟器的班级项目创建的。

    bitwise_operations_cpp:按位运算

    按位运算符 操作员 意义 描述 和 按位与 对两个数字的每一位执行与运算。 仅当两个位均为1时才为1 | 按位或 对两个数字的每一位进行“或”运算。 如果两个位中的任何一位为1,则为1。 ^ 按位异或 对两个数字的...

    基于opencv3.1库的JAVA源码

    第1章 Java概述、安装及简易教学 14 1-1 Java概述 14 1-2 Java安装 16 1-3 Eclipse安装 18 1-4 GUI设计工具WindowBuilder 18 1-5 在Eclipse开发第一个Java程式 23 1-6 在Eclipse开发第一个Java视窗程式-显示影像 26 ...

    vs没报错leetcode报错-JavaDataStructure_Algorithm:这个repo包含我目前正在学习的java笔记和leet

    按位运算符 按位运算符 象征 和 & 或者 一世 异或 ^ &lt;&lt; 有符号左移 &gt;&gt; 签名右移 &lt;&lt;&lt; Java 中没有无符号左移 &gt;&gt;&gt; 无符号右移 汉明权重 - 它包含的设置位数的计数 词汇 设置位 - 1 清除位 - 0 最重要的...

    C程序设计语言第2版

    Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and Expressions Conditional Expressions Precedence and Order of ...

    bitwise-rotation:values按位旋转值

    在计算机编程中,循环移位(或按位旋转)是将其操作数的所有位移位的移位运算符。 与算术移位不同,循环移位不会保留数字的符号位,也不会将数字的指数与其有效位数(有时称为尾数)区分开。 —如何使用import ...

    The C Cheat Sheet - An Introduction to Programming in C

    4.5 Bitwise Operators 4.6 Relational Operators 4.7 Logical Operators 4.8 The Conditional Operator The C Cheat Sheet Revision 1 Copyright ? 2000 Andrew Sterian iv 4.9 The Comma Operator 4.10 ...

    The_C_Programming_Language-英文版

    2.6 Relational and Logical Operators 39 2.7 Type Conversions 40 2.8 Increment and Decrement Operators 43 2.9 Bitwise Operators 45 2.10 Assignment Operators and Expressions 46 2.11 Conditional ...

    bitwise manipulation

    A test harness that checks a student's solution in bits.c

    cpp-Bitwise是一个教育项目能让我们从头开始为计算机创建软件硬件堆栈

    Bitwise是一个教育项目,能让我们从头开始为计算机创建软件/硬件堆栈

    bitwise:JavaScriptTypeScript库可操作位,半字节,字节和缓冲区

    按位 JavaScript / TypeScript库可操作位,半字节,字节和缓冲区。 例子 import bitwise from 'bitwise' const bits = bitwise . byte . read ( 42 ) // [0, 0, 1, 0, 1, 0, 1, 0] bitwise . bits . toString ( ...

    bitwise.zip

    bitwise.zip

    bitwise-operations

    启用按位操作的运算符的说明性示例。 该程序用C ++编写,以便std::bitset STL std::bitset轻松显示位。 安装 不包括makefile,只需使用您最喜欢的C ++编译器进行编译并运行即可。 样本输出 ---- BITWISE ...

    Thinking in Java 4th Edition

    Logical operators .................. 71 Short-circuiting ............................ 72 Literals .................................. 73 Exponential notation ................... 74 Bitwise operators .....

    Leetcode 数字范围按位与.pas

    LeetCode 201的题目是"数字范围按位与"(Bitwise AND of Numbers Range),其核心要求是给定两个整数(记为m和n),需要找出这两个数范围内所有数的按位与(bitwise AND)结果。例如,给定范围[5, 7],结果应为4。 ...

Global site tag (gtag.js) - Google Analytics