Monday, March 30, 2009

Bitwise Operator :Introduction

Java defines several types of Bitwise operators which are applicable to integer type,long,int ,short,byte,char values.These operators act on each and every bit of their operands.

Bitwise Operators are listed as following :-




Bitwise operators manipulate the bits within an integer value,so it is important to know what effects the changes will bring on integer value.It is also useful to know how Java stores integer values and the way negative integer values are represented.

All integer values in Java are represented by binary numbers of varying lengths.For example a byte value for 22 in binary is 00010101.The formula used to convert binary to integer would be as example tells:-in above case the binary to integer conversion takes place as

16+4+1=21

the theory is get all bits whose value is 1, count its position from right and say it "n" and compute "pow(2,n-1)" thus here first 1 comes at 1st position thus giving value pow(2,1-1)=pow(2,0)=1
Similarly for second you get as poe(2,2)=4 as it appears on 3rd position.

All integer types are signed integers due to which they can either represent a negative integer or positive value.Java use two's complement method for representing negative numbers.Lets see how 2's Complement works.Take binary string for a negative number assume -21 be the number.
First invert all bit values ,that is change 0 to 1 and vice-versa.Then add 1 to its rightmost bit.

21 is represented as 00010101 in binary string.

ENCODING FOR 2'S COMPLEMENT
Step 1: Invert all individual bits we get 11101010
Step 2: Add 1 to Rightmost bit that is

11101010

+1
---------------------
11101011 it is binary value for -21
---------------------
DECODING 2'S COMPLEMENT

of -21 (11101011)
Step 1: Invert all individual bits here we get 00010100 which is equal to 20.
Step 2: Add 1 to rightmost bit

00010100
+1
-------------------
00010101 and it is equal to 21
--------------------

IMPORTANT:-Java uses 2's complement to store negative numbers and because all integers are signed values in Java thus applying them may produce unexpected outcomes.Consider for example ,turning on higher order bits will cause resulting values to be interpreted as negative .To avoid such situations always keep in mind that higher order bits determines the sign of an integer value no matter what how that bit is set.

Useful Links :-

Assignment Operators
Arithmetic Operators
Logical operators
Relational Operators
Conditional Operators
Bitwise Logical operator
Bitwise Left shift Operator
Bitwise Right shift Operator

Sponsored Links :-

Get Your SCJP preparation Kit free!!!!!!!
Get Your SCWCD preparation Kit free!!!
Are you Intelligent check here

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP