This week's giveaway is in the Spring forum.We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of REST with Spring (video course) this week in the Spring forum!

# The OR opertor doubt

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
What is the result of the following operation?

System.out.println(4 | 3);

1) 6
2) 0
3) 1
4) 7

here i dont know how to convert the 4 and 3 to binary ?

Please tell me the procedure for converting number to binary one?
I am weak in this one.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The OR operation works on bits. If either bit of two in an OR operation is 1, then the result is 1, so:

4 = 100
3 = 011
--------
111

so the result must be 7 (or 4 + 2 + 1).

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
how did you convert the 4 to bits representation?
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
To Convert any decimal number into binary
we can folow these steps

Lets Do it With An Exammple
decimal 22 into binary

make a table like this
1
2
4
8
16
32
64
..
..
..

if u wanna convert 22 look for the number or the number just smaller than this,so what i see that the table does not have 22 so the number next smaller than this is 16,write 1 in front of it

1
2
4
8
16--1
32
64
..
..
..

ok next step reduce 16 from the number 22,22-16=6
now we get the number 6 repeat the same procedure,we do not have 6 in the table but do have 4, write 1 in front of it

1
2
4 --1
8
16--1
32
64
..
..
..
now the repeat the same deduct 4 out of 6 ,6-4=2 you got 2 which is in the table,write 1 in front of it

1
2 --1
4 --1
8
16--1
32
64
..
..
..

now when u deduct you 0,2-2=0 so ur binary is complete
now to the remaining table numbers put a 0 in front of it

1 --0
2 --1
4 --1
8 --0
16--1
32
64
..
..
..
now just read the number from down to up,10110 here is your binary conversion
hope you understand this easily

Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

This might help too:

http://www.is.wayne.edu/olmt/binary/page3.htm
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Mr. Michael
I wanna Know How 2s Complement Representation Works
thanks With Regards

3rd Year
B.Tech
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
Thanks a lot very much

i am gratefull to all of you

I agree. Here's the link: http://aspose.com/file-tools

subject: The OR opertor doubt