`
Data168
  • 浏览: 3174 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Bitwise and Bit Shift Operators in Java

    博客分类:
  • Java
 
阅读更多

按位与运算符(&)

参加运算的两个数据,按二进制位进行“与”运算。

运算规则:0&0=0;   0&1=0;    1&0=0;     1&1=1;

       即:两位同时为“1”,结果才为“1”,否则为0

例如:3&5  即 0000 0011 & 0000 0101 = 0000 0001   因此,3&5的值得1。

 

另,负数按补码形式参加按位与运算。

“与运算”的特殊用途:

(1)清零。如果想将一个单元清零,即使其全部二进制位为0,只要与一个各位都为零的数值相与,结果为零。

 

(2)取一个数中指定位

方法:找一个数,对应X要取的位,该数的对应位为1,其余位为零,此数与X进行“与运算”可以得到X中的指定位。

例:设X=10101110,

    取X的低4位,用 X & 0000 1111 = 0000 1110 即可得到;

    还可用来取X的2、4、6位。

 

按位或运算符(|)

参加运算的两个对象,按二进制位进行“或”运算。

运算规则:0|0=0;   0|1=1;   1|0=1;    1|1=1;

      即 :参加运算的两个对象只要有一个为1,其值为1。

例如:3|5 即 0000 0011 | 0000 0101 = 0000 0111   因此,3|5的值得7。 

 

另,负数按补码形式参加按位或运算。

“或运算”特殊作用:

(1)常用来对一个数据的某些位置1。

方法:找到一个数,对应X要置1的位,该数的对应位为1,其余位为零。此数与X相或可使X中的某些位置1。

例:将X=10100000的低4位置1 ,用 X | 0000 1111 = 1010 1111即可得到。

 

异或运算符(^)

参加运算的两个数据,按二进制位进行“异或”运算。

运算规则:0^0=0;   0^1=1;   1^0=1;   1^1=0;

   即:参加运算的两个对象,如果两个相应位为“异”(值不同),则该位结果为1,否则为0。

 

“异或运算”的特殊作用:

(1)使特定位翻转 找一个数,对应X要翻转的各位,该数的对应位为1,其余位为零,此数与X对应位异或即可。

例:X=10101110,使X低4位翻转,用X ^ 0000 1111 = 1010 0001即可得到。

 

(2)与0相异或,保留原值 ,X ^ 0000 0000 = 1010 1110。

从上面的例题可以清楚的看到这一点。

取反运算符(~)

参加运算的一个数据,按二进制位进行“取反”运算。

运算规则:~1=0;   ~0=1;

      即:对一个二进制数按位取反,即将0变1,1变0。

 

使一个数的最低位为零,可以表示为:a&~1。

~1的值为1111111111111110,再按“与”运算,最低位一定为0。因为“~”运算符的优先级比算术运算符、关系运算符、逻辑运算符和其他运算符都高。

左移运算符(<<)

将一个运算对象的各二进制位全部左移若干位(左边的二进制位丢弃,右边补0)。

例:a = a << 2 将a的二进制位左移2位,右补0,

左移1位后a = a * 2; 

若左移时舍弃的高位不包含1,则每左移一位,相当于该数乘以2。

右移运算符(>>)

将一个数的各二进制位全部右移若干位,正数左补0,负数左补1,右边丢弃。

操作数每右移一位,相当于该数除以2。

例如:a = a >> 2 将a的二进制位右移2位,

左补0 or 补1 得看被移数是正还是负。

 

 

>> 运算符把 expression1 的所有位向右移 expression2 指定的位数。expression1 的符号位被用来填充右移后左边空出来的位。向右移出的位被丢弃。

例如,下面的代码被求值后,temp 的值是 -4:

  -14 (即二进制的 11110010)右移两位等于 -4 (即二进制的 11111100)。

  var temp = -14 >> 2

 

 

  无符号右移运算符(>>>)

 

 

>>> 运算符把 expression1 的各个位向右移 expression2 指定的位数。右移后左边空出的位用零来填充。移出右边的位被丢弃。

例如:var temp = -14 >>> 2

变量 temp 的值 -14 (即二进制的 11111111 11111111 11111111 11110010),向右移两位后等于 1073741820 (即二进制的 00111111 11111111 11111111 11111100)。

复合赋值运算符

位运算符与赋值运算符结合,组成新的复合赋值运算符,它们是:

&=    例:a &= b        相当于a=a & b

|=    例:a |= b        相当于a=a | b

>>=   例:a >>= b       相当于a=a >> b

<<= 例:a <<= b       相当于a=a << b

^=   例:a ^= b       相当于a=a ^ b

运算规则:和前面讲的复合赋值运算符的运算规则相似。

不同长度的数据进行位运算

如果两个不同长度的数据进行位运算时,系统会将二者按右端对齐,然后进行位运算

以“与”运算为例说明如下:我们知道在C语言中long型占4个字节,int型占2个字节,如果一个long型数据与一个int型数据进行“与”运算,右端对齐后,左边不足的位依下面三种情况补足,

(1)如果整型数据为正数,左边补16个0。

(2)如果整型数据为负数,左边补16个1。

(3)如果整形数据为无符号数,左边也补16个0。

如:long a=123;int b=1;计算a & b。

 

如:long a=123;int b=-1;计算a & b。

 

如:long a=123;unsigned int b=1;计算a & b。

 

(本文转载自:leesa的博客

分享到:
评论

相关推荐

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

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

    The C Cheat Sheet - An Introduction to Programming in C

    比较简短的C语言教程,英文资料 1.0 Introduction 1.1 The main() Function 1.2 Include Files 1.3 Whitespace 1.4 The Preprocessor 1.4.1 The #define Directive ...11.0 Differences between Java and C

    The C programming Language(chm格式完整版)

    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 -...

    C程序设计语言第2版

    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 -...

    Thinking in Java 4th Edition

    Arrays in Java .............................. 44 You never need to destroy an object .................. 45 Scoping ........................................ 45 Scope of objects ...........................

    Programming Interview Problems and Algorithms in Ruby

    Programming Interview Problems and Algorithms in Ruby by Zachary Paul English | April 17, 2016 | ASIN: B01EGILLLS | 177 Pages The book covers a large number of the most common interview problems, as ...

    Programming in Objective-C 4th Edition

    Bit Operators 211 The Bitwise AND Operator 212 The Bitwise Inclusive-OR Operator 213 The Bitwise Exclusive-OR Operator 214 The Ones Complement Operator The Left Shift Operator 216 The Right Shift ...

    JavaScript Data Structures and Algorithms

    Review core algorithm fundamentals: search, sort, recursion, breadth/depth first search, dynamic programming, bitwise operators Examine how the core data structure and algorithms knowledge fits into ...

    Programming.in.C.2nd.Edition..0198065280.

    Some advanced features of C such as memory models, command-line arguments, and bitwise operators have also been included. Case studies demonstrating the use of C in solving mathematical as well as ...

    Programming in C(OXFORD,2ed,2012)

    Some advanced features of C such as memory models, command-line arguments, and bitwise operators have also been included. Case studies demonstrating the use of C in solving mathematical as well as ...

    Practical C++ Programming C++编程实践

    Bit Operations Bit Operators The AND Operator (&) Bitwise OR (|) The Bitwise Exclusive OR (^) The Ones Complement Operator (NOT) (~) The Left and Right Shift Operators (&gt;) Setting, Clearing, and ...

    The_C_Programming_Language-英文版

    2.9 Bitwise Operators 45 2.10 Assignment Operators and Expressions 46 2.11 Conditional Expressions 47 2.12 Precedence and Order of Evaluation 48 Chapter 3 - Control Flow 50 3.1 Statements and Blocks ...

    bitwise manipulation

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

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

    按位 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] //

    8051助记符/地址对照表

    JBC - Jump if Bit Set and Clear Bit JC - Jump if Carry Set JMP - Jump to Address JNB - Jump if Bit Not Set JNC - Jump if Carry Not Set JNZ - Jump if Accumulator Not Zero JZ - Jump if Accumulator Zero ...

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

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

    RBDS.rar_Bitwise operation_operation_rbds

    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.

    bitvector:人类比特矢量:trade_mark:

    人类位向量:trade_mark: 这个简单的位向量实现旨在使寻址单个位不那么繁琐。...&gt; power = BitField ( 0 , 1 ) # offset and size &gt; spin = BitField ( 1 , 1 ) &gt; speed = BitField ( 2 , 4 ) &gt; sense = BitField

    bitwise-operations

    按位运算 启用按位操作的运算符的说明性示例。 该程序用C ++编写,以便std::bitset STL std::bitset轻松显示位。 安装 不包括makefile,只需使用您最喜欢的C ++编译器进行编译并...Use bitwise AND & to switch off byt

Global site tag (gtag.js) - Google Analytics