• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Confused with logic

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,
The below code prints the binary equivalent of int given as arg
to the method.Can someone, please help me with the logic applied. Particularly with line no.5
void pBinInt(String s, int i) {
System.out.println(s + ", int: " + i + ", binary: ");
System.out.print(" ");
for(int j = 31; j >=0; j--)
if(((1 << j) & i) != 0) /*line no.5*/
System.out.print("1");
else
System.out.print("0");
}
Thnx in advance.
Bindesh Vijayan
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijayan,

Let us go thru your code


Your code works like this
Iter 1 : 1<< 31 = 10000000 00000000 00000000 00000000
i = 5 = 00000000 00000000 00000000 00000101
(1<<31) & 5= 00000000 00000000 00000000 00000000 This exp results line no 8 ie 0

Iter 2 : 1<< 30 = 010000000 00000000 00000000 0000000
i = 5 = 00000000 00000000 00000000 00000101
(1<<30) & 5= 00000000 00000000 00000000 00000000 This exp results line no 8 ie 0
Like this the for loop executes until j becomes 0. Thus the prg is printing the binary value for any given int.
Hope this helps
Indu

[This message has been edited by INDU, BALA (edited August 24, 2001).]
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Indu,
Although I understood the working of code.The thing that i could'nt understand was that why are we ((1 << j) & i) doing this ? What is the logic behind this expression?
Please help.
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Probably I found an answer,what is happening is
(((1 << j) & i) != 0)
suppose,i=5
Iter 1: (1<<0)

1)1's binary reprsentation: 00000000 00000000 00000000 00000001<<0 (no changes here)
2) Binary AND: 00000000000000000000000000000001 & 00000000000000000000000000000101=
00000000000000000000000000000001 (a non-zero value) thus it prints 1.
The logic: In each iteration a bit gets left shifted in 1 (1<<j) so that there is exactly one 1 bit in binary representation of 1(left operand). When we AND it with 5's binary repr.,the 1's bit in the 1(left operand), extract's the corresponding 1 bit in 5(if any).This results in, as a non-zero value for the Not Equal to(!=) operator,thus, 1 gets printed.Let's look at it more elaborately(remember iam still in 1st iter):->

00000000000000000000000000000001(at leat one 1 bit in binary),
AND 00000000000000000000000000000101
----------------------------------------
00000000000000000000000000000001 (non- zero result),prints 1
Iter 2 1<<1)
00000000000000000000000000000010
AND 00000000000000000000000000000101
----------------------------------------
00000000000000000000000000000000 (zero result),prints 0
Iter 3 1<<2)
00000000000000000000000000000100
AND 00000000000000000000000000000101
------------------------------------------
00000000000000000000000000000100
and so on
Please correct me if wrong.
THANKS.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the line 5 it is a mask which is generated and if the value is greater than 0 the bit is high for that particular mask and the mask is changing with the value of j.
i hope i'm understanding what u mean.
thanks.
Jennifer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic