JAVA Bitwise Logical Operator

This tutorial will take you step by step through the process of understanding and using operators that act on individual bits. The best way to learn is to compile and run these programs yourself (copy, paste, compile and run !). Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

The bitwise logical operators are : ~ , | , & , ^

~ the NOT operator

| the OR operator

& the AND operator

^ the XOR operator

These opearators act on each individual bit within each opearand according to this table

A       B       A|B     A&B     A^B     ~A



0       0        0       0       0       1 1       0        1       0       1       0 0       1        1       0       1       1 1       1        1       1       0       0

Example 1: (~) the NOT opearator

This example shows the effect of using the ~ opearator.

  

class Bits1{ 

   public static void main(String args[]){

      System.out.println(" ~ NOT opeartor");



// invert 0

      System.out.println("~ 0 = "+~0); 



// invert 11

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

    }

}



Explanations of ~0 = -1.

0 in binary is: 00000000000000000000000000000000

invert all the bits: 11111111111111111111111111111111

This is a negative number since its bit on the left is 1. To find its value we use the two's complement method.

invert all bits and add 1 and this gives

00000000000000000000000000000001, this is the binary form of 1. so 11111111111111111111111111111111 is the binary form of -1

Explanation of ~11 = -12

11 in binary is: 00000000000000000000000000001011

invert all bits: 11111111111111111111111111110100, this is a negative number and we use the two's complement method to fint its value.

invert all bits

00000000000000000000000000001011

add 1: 00000000000000000000000000001100 this is 12 (1*2^3+1*2^2+0*2^1+0*2^0)

and so 11111111111111111111111111110100 corresponds to -12

Example 2: ( | ) the OR opearator

This example shows the effect of using the | opearator.

  

class Bits2{ 

   public static void main(String args[]){

      System.out.println(" | OR opeartor");



// apply the | operator

      int x = 11|10;

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

 }   }



Explanation of 11|10 = 11

11 in binary is: 00000000000000000000000000001101

10 in binary is: 00000000000000000000000000001100

apply the | operator to each individual bit and you obtain

00000000000000000000000000001101
in decimal form this number is: 1*2^3+1*2^2+0*2^1+1*2^0 = 11

Example 3: ( & ) the AND opearator

This example shows the effect of using the AND opearator.

  

class Bits3{ 

   public static void main(String args[]){

      System.out.println(" & OR opeartor");



// apply the & operator

      int x = 11&10;

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

 }   }



Explanation of 11&10 = 10

11 in binary is: 00000000000000000000000000001101

10 in binary is: 00000000000000000000000000001100

apply the & operator to each individual bit and you obtain

00000000000000000000000000001100
in decimal form this number is: 1*2^3+1*2^2+0*2^1+0*2^0 = 10

Example 4: ( ^ ) the XOR opearator

This example shows the effect of using the XOR opearator.

  

class Bits3{ 

   public static void main(String args[]){

      System.out.println(" ^ XOR opeartor");



// apply the ^ operator

      int x = 11^10;

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

 }   }



Explanation of 11^10 = 1

11 in binary is: 00000000000000000000000000001101

10 in binary is: 00000000000000000000000000001100

apply the ^ operator to each individual bit and you obtain

00000000000000000000000000000001
in decimal form this number is: 1*2^0 = 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值