Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

bitwise question

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am taking the sample test in the CD back of K&B book
the quesiton on bitwise that i have:

public class Bitwise
{
public static void main (String [] args)
{
int x= 12 & 13;
int y= x ^ 6;
System.out.println(y | 3);
}

The answer is 11.
Can anyone giving me more explaination, do i need to convert 12 and 13 into binary and then doing the &, and get the result plug in x ^ 6, convert 6 into binaray,
Greately appreciate if giving detail.
Regards,
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the &, ^, and | operators here are bitwise operators. They operate on the bits.

12 = 1100
13 = 1101
6 = 0110
3 = 0011

So 12 & 13 = 1100
Then 1100 ^ 0110 = 1010
Then 1010 | 0011 = 1011
and 1011 is binary for (int) 11.

_M_
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exactly....for bitwise operators...u should convert into binary representation and calculate accordingly...there might be other procedures as well .............so binary representation for 12 can be taken as 1100 (last four bits are considered as before bits are all 0's...just as it is easy for calculation)and for 13 it is 1101
and 12&13 means
1100
1101
-----
1100 =x (as in the code)
-----
as '&' means it give 1 if both bits are 1 other wise 0;

next y=x^6
1100
0110
-----
1010 = y( as in the code)
-----
since '^' means xor operator which give 1 if and only if one of the bits are 1 otherwise 0

next printing y|3
from above y is 1010
and 3 will be 0011

1010
0011
-----
1011 (result)
----
so if u convert this bin rep into decimal..u will get 11 as the answer....

ranchers correct me if i am wrong....
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for quick reply
how do i convert 1011 into back to decimal.
thnks
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1011 in binary means 1 in the 8's place, 0 in the 4's place, 1 in the 2's place, and 1 in the 1's place. To convert to decimal you do:

8 + 2 + 1 = 11.

You should probably learn how to do simple conversions between binary, decimal, and hexidecimal. It's a handy skill to have. Most programmers have memorized all of the 4-bit patterns (there are 16).

_M_
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks again Mike
 
crispy bacon. crispy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic