Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Bitwise logical operators in Java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-11-2009
Member
 
Join Date: May 2008
Posts: 69
Bitwise logical operators in Java
  

Hi Friends,

I am studying in second year of the Bsc(I.T). Portion of this year includes JAVA programming.The main thing is that I don't know anything about the bitwise logical operators.

Can anyone suggest me, what are the different Bitwise logical operators are available in JAVA language? What is the use of them?

Reply With Quote
  #2  
Old 20-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Bitwise logical operators in Java

Following are the different bitwise logical operators available in Java:


1. AND(&):1 if both bits are 1.
e.g:3 & 5 (result is 1 )

2.XOR(^):1 if both bits are different.
e.g: 5 ^7 (result is 6)

3. OR(|):1 if either bit is 1.
e.g:3 | 5 (result is 7)

4.Shift Left(<<):This operator used to Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.
e.g:3 << 2(result is 12)
Reply With Quote
  #3  
Old 20-11-2009
Member
 
Join Date: May 2008
Posts: 2,381
Re: Bitwise logical operators in Java

According to my knowledge following Bitwise logical operators are available in the Java:

i). NOT operator(~)

ii). OR operator(|)

iii). AND operator(&)

iii). XOR operator(^)

For reference see below example of 'AND' operator:

Quote:
class Bits3{
public static void main(String args[]){
System.out.println(" & OR opeartor");

// apply the AND(&) operator
int a = 21&40;
System.out.println("21&40 = "+a);
} }
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 20-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,845
Re: Bitwise logical operators in Java

Bitwise logical operators of java operate on individual bits of integer (long & int) values. Suppose If an operand is shorter than an int, it is promoted to int before doing the any operation.


Important use of the bitwise logical operators is to work with multiple values that have been encoded within one int. See below example of this:

Quote:
int Age, Gen, Ht;
short packed_info;
. . .
// packing
packed_info = (((Age<< 1) | Gen) << 7) | Ht;
. . .
// unpacking
Ht= packed_info & 0x7f;
Gen= (packed_info >>> 7) & 1;
Age= (packed_info >>> 8);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Bitwise logical operators in Java"
Thread Thread Starter Forum Replies Last Post
Use of Bitwise Logical Operators : C Gavyn Software Development 4 06-02-2010 03:25 PM
How to use Comparison and Logical Operators in JavaScript? Beter 2 Burn Out Software Development 5 30-01-2010 06:45 PM
Bitwise Concatenation In Int Aakarshan.d Software Development 5 25-01-2010 01:41 PM
Don't know about short-circuit operators of java Ram Bharose Software Development 5 20-01-2010 07:26 AM
Memory allocation optimized bitwise Ricky58 Software Development 3 07-05-2009 10:53 AM


All times are GMT +5.5. The time now is 11:27 PM.