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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Confused with logic

Ranch Hand
Posts: 104
• Number of slices to send:
Optional 'thank-you' note:
Hi Everybody,
The below code prints the binary equivalent of int given as arg
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");
}
Bindesh Vijayan

Ranch Hand
Posts: 48
• Number of slices to send:
Optional 'thank-you' note:
Hi Vijayan,

Let us go thru your code

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
• Number of slices to send:
Optional 'thank-you' note:
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?

Bindesh Vijayan
Ranch Hand
Posts: 104
• Number of slices to send:
Optional 'thank-you' note:
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
THANKS.

Ranch Hand
Posts: 53
• Number of slices to send:
Optional 'thank-you' note:
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.

 Don't get me started about those stupid light bulbs.