Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The OR opertor doubt

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how did you convert the 4 to bits representation?
 
anand phulwani
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help too:

http://www.is.wayne.edu/olmt/binary/page3.htm
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Michael
I wanna Know How 2s Complement Representation Works
thanks With Regards





Agrah Upadhyay
3rd Year
B.Tech
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot very much

i am gratefull to all of you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic