Java SE: Bitwise and Bit Shift Operations

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:

OperatorNameExampleResultDescription
a & band3 & 511 if both bits are 1.
a | bor3 | 571 if either bit is 1.
a ^ bxor3 ^ 561 if both bits are different.
~anot~3-4Inverts the bits.
n << pleft shift3 <<< 212Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.
n >> pright shift5 >> 21Shifts 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 >>> pright shift-4 >>> 2815Shifts 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值