Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

truning numbers to binary

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,
I am not getting this into my head..... can someone explain.
What is the result of the following operation?
System.out.println(4 | 3);
Answer is 7 HOW???
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Anjali Purohit:
Folks,
I am not getting this into my head..... can someone explain.
What is the result of the following operation?
System.out.println(4 | 3);
Answer is 7 HOW???


4 in binary = 0100
3 in binary = 0011
0100 | 0011
4: 0100
3: 0011
result: 0111
which "0111 = 7"
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anjali
4|3 returns 7 as '|' is the Bitwise inclusive OR operator and the rule for this operator dictates if either of the bits are 1 then the result is 1. You could think of it as a light which is controlled by two switches. So if either of the switches are are on the light will be on (ie 1). Therefor working along from this rule...
0000 0100 // 4 in binary used bytes here for berevity!
| 0000 0011 // 3 in binary
------------
0000 0111 // 7 in binary as if either of the bits with OR are 1 then result is 1
hope this helps - Jim
 
Anjali Purohit
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I wanted to know is
4 in binary = 0100 and
3 in binary = 0011 HOW??
In the exam how do I decipher that 4's binary is 0100 & 3's is 0011? Explain here how do I convert the number into its binary equivalent.
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Anjali Purohit:
What I wanted to know is
4 in binary = 0100 and
3 in binary = 0011 HOW??
In the exam how do I decipher that 4's binary is 0100 & 3's is 0011? Explain here how do I convert the number into its binary equivalent.


really easy..
take a look at this link http://www.javaranch.com/campfire/StoryBits.jsp
 
Anjali Purohit
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot FEI NG & Jim. I was getting worked up over the shift operators but now things are clear to me. I don't know how I missed to check that link on javaranch.
Anyway, could u further explain how to change binary to a number.....0111 to 7.
Thanx again.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this post.
HTH,
- Manish
 
Ranch Hand
Posts: 5390
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi anjali
for any base number rule is simple
if there is a number with m digits in base n then number is
mth digit * n^(m-1) + (m-1)th digit * n^(m-2) + ......
in simple words from binay to decimal:
if binary no(base 2) is 0111 then
(0 * 2^3) + (1 * 2^2 ) + (1 * 2^1) + (1 * 2^0)
= (0 * 8) + (1 * 4) + ( 1 * 2) + ( 1 * 1)
= 0 + 4 + 2 + 1
= 7

for Decimal(base 10) 357:
(3 * 10^2) + (5 * 10^1) + (7 * 10^0)
= 300 + 50 + 7
= 357
 
Anjali Purohit
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot manish & ravish. That's cleared the cobwebs from my mind.
 
Space pants. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!