Thursday, July 9, 2009

In java, static int display() { int a=7,b=8,c; c=a&b; return c; } this gives 0 . why?

a=7 it converts into binary value as 0111 then


b=8 it converts into binary value as 1000


because of operation is %26amp; and


means a%26amp;b;


for this operation complier calculates in binary form only


so a=1000


b=0111


---------------


a%26amp;b=0000 hear compare bit by bit





Table


a=0 b=0 a%26amp;b=0


a=0 b=1 a%26amp;b=0


a=1 b=0 a%26amp;b=0


a=1 b=1 a%26amp;b=1


according to the above table the answer is:0


a%26amp;b=0000 it converts into decimal form then gives o/p as :0

In java, static int display() { int a=7,b=8,c; c=a%26amp;b; return c; } this gives 0 . why?
(ignoring actual representation...) 8 is 1000, 7 would be 0111; logical AND on those two sequences would result in 0000.
Reply:maybe you want to change it to





c=a+b; ?





you don't need the c variable anyway, just





return a%26amp;b; //(or whatever you want)


No comments:

Post a Comment