The Artima Developer Community
Sponsored Link

Java Buzz Forum
Bitwise operators in java with example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Bitwise operators in java with example Posted: Apr 28, 2016 1:06 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Bitwise operators in java with example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Bitwise operators in java performs operation on bits 
  • Bitwise operators performs bit by bit operations
  • Bit-wise operators operates on individual bits of operands.


 Bit wise Operators in Java:

  1. ~      : Bit wise unary not
  2. &     : Bit wise And
  3. |       : Bit wise OR
  4. ^      : Bit wise Exclusive OR


 1.Bit wise Unary Not  ~:

  • Bit wise Unary not Inverts the bits.
  • Lets see a java program on bit wise unary not

Java Example program on bit wise unary not operator ~:

  1. package operatorsinjava;
  2. public class BitwiseUnaryNotDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7.     public static void main(String[] args) {
  8.  
  9.   int x=2;
  10.  
  11.   System.out.println(~x);
  12.         
  13.   int y= 3;
  14.  
  15.   System.out.println(~y);
  16.         
  17.   int z= 4;
  18.         
  19.   System.out.println(~z);
  20.  
  21.     }
  22.  
  23. }

Output:


  1. -3
  2. -4
  3. -5

bitwise unary operator in java


2. Bitwise AND operator:

  • Bit wise And returns 1 if both operands position values are 1 otherwise 0.
  • 1 & 2
  • 001
    010
    -----
    000 =0
    -----
  • 2 & 3
  • 010
    011
    -----
    010 =2
    -----
Java Example program on bitwise AND operator &:


  1. package operatorsinjava;
  2. public class BitwiseAndDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {

  8.    int x=1;
  9.    int y=2;
  10.         
  11.   System.out.println(x&y);
  12.         
  13.   int i=2;
  14.   int j=3;
  15.         
  16.  System.out.println(i&j);
  17.         
  18.  System.out.println(4&5);
  19.  
  20. }
  21.  
  22. }

Output:


  1. 0
  2. 2
  3. 4
Bitwise And operator in java Example


3.Bitwise OR operator in Java | :

  • Bit wise OR returns 1 if  any one operands position values are 1 or both 1 s. otherwise 0.
  • 1 |2
  • 001
    010
    -----
    011 =3
    -----
  • 2 | 3
  • 010
    011
    -----
    011 =3
    -----
Java Example program on bitwise OR operator |:

  1. package operatorsinjava;
  2. public class BitwiseORDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {

  8.    int x=1;
  9.    int y=2;
  10.         
  11.   System.out.println(x|y);
  12.         
  13.   int i=2;
  14.   int j=3;
  15.         
  16.  System.out.println(i|j);
  17.         
  18.  System.out.println(4|5);
  19.  
  20. }
  21.  
  22. }

Output:

  1. 3
  2. 3
  3. 5

4.Bit wise Exclusive OR operator in java ^:

  • Bit wise XOR operator returns 1 if any of the operands bits position 1 
  • Java XOR operator returns 0 if both operands position is 1.
  • Lest see java Xor on 1 ^ 2
  • 1 ^ 2
  • 001
    010
    -----
    011 =3
    -----
  • Lest see java xor operation on 2 ^3
  • 2 ^ 3
  • 010
    011
    -----
    001 =1
    -----

Java Example program on bitwise OR operator |:

  1. package operatorsinjava;
  2. public class BitwiseXORDemo {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {

  8.    int x=1;
  9.    int y=2;
  10.         
  11.   System.out.println(x^y);
  12.         
  13.   int i=2;
  14.   int j=3;
  15.         
  16.  System.out.println(i^j);
  17.         
  18.  System.out.println(4^5);
  19.  
  20. }
  21.  
  22. }

Output:

  1. 3
  2. 1
  3. 1


Java XOR bitwise operator

Read: Bitwise operators in java with example

Topic: JavaFX Applications with e(fx)clipse Previous Topic   Next Topic Topic: Ternary operator in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use