Total members 11895 |It is currently Mon Dec 30, 2024 5:56 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






Bitwise logical operators in java

Bitwise operators is used to control values of primitive data-types such as long, integer, short and byte. These operators do changes to the variables by doing some action and manipulation on bits level. There are four defined bitwise operators in java which are : AND operator, OR operator, XOR operator and complement operator. The first three operators are binary which means that they need two operands while the last one is unary operator which means it needs only one operand. Next will explain the usage of each operator by example.

The AND operator : (&)


The AND(&) logical operator gives one (1) in only case one case where the two inputs
Code:

x           y                 x&y 
-----------------------------
0           0                  0
0           1                  0
1           0                  0
1           1                  1


Example using & operator on bytes
java code
byte x = 50;
byte y = 51;
byte result = (byte) (x&y);
System.out.println("Result of x&y= : " + result );


The result equal = 50
which is in binary:
Code:
00110010
00110011
-------------
00110010

& operator on Integers:
java code
int xInt = 30;
int yInt = 31;
int result = (xInt&yInt);
System.out.println("Result of x&y (Integers)= : " + result );

The output is :
Code:
Result of x&y (Integers)= : 30

The OR operator : (|)


The OR operators gives one(1) if any of the two inputs equals one, zero otherwise.
Code:

x           y                 x|y 
-----------------------------
0           0                  0
0           1                  1
1           0                  1
1           1                  1


Example on |(OR) operator on bytes

java code
byte x = 50;
byte y = 51;
byte result = (byte) (x|y);
System.out.println("Result of x|y= : " + result );

The output is :
Code:
The result equal = 51


Code:
00110010
00110011
-------------
00110011

Example on |(OR) operator on integers
java code
int xInt = 30;
int zInt=33;
int result = (xInt|zInt);
System.out.println("Result of x|y (Integers)= : " + result );

The output is :
Code:
Result of x|y (Integers)= : 63


The XOR operator : (^)


The XOR(^) operator gives one(1) if the inputs are different, otherwise zero.
Code:

x           y                 x^y 
-----------------------------
0           0                  0
0           1                  1
1           0                  1
1           1                  0


Example on ^ operator on bytes

java code
byte x = 50;
byte y = 51;
byte result = (byte) (x^y);
System.out.println("Result of x^y= : " + result );


The result equal = 1
Code:
00110010
00110011
-------------
00000001

Example on ^ operator on Integers
java code
int xInt = 30;
int zInt=31;
int result = (xInt^zInt);
System.out.println("Result of x^y (Integers)= : " + result );

The output is :
Code:
Result of x^y (Integers)= : 1

The Inversion Operator: (~)


Invert each bit in the byte (one's complement of bits array).

Example :
Code:
~00110010 = 11001101

Invert operator on integer
java code
int xInt = 30;
int result = ~xInt;
System.out.println("Result of ~xInt(Integers)= : " + result );

The output is :
Code:
Result of ~xInt(Integers)= : -31

Boolean Inversion Operator (!) invert the value of boolean variable



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Bitwise operators in java
 Bitwise operators     -  
 bitwise operators usage in C++     -  
 Comparison Operators in java     -  
 Arithmetic Operators in java     -  
 working of bitwise ~ operator     -  
 PHP operators     -  
 C++ Increment Operators     -  
 using bit-wise operators in c++     -  
 difference between the >> and >>> operators     -  
 Using Assignment Operators     -  



Topic Tags

Java Basics
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com