bala_chocos

Ranch Hand

Posts: 48

posted 17 years ago

hi

please explain the answer for the question given below

Q5:Given two int variables initialized as follows:

int a=63;// bit pattern //////

int b=4;// bit pattern 000100

what is the value of a&b.

a).63

b).4

c).null

d).0

answer:b

please explain the answer for the question given below

Q5:Given two int variables initialized as follows:

int a=63;// bit pattern //////

int b=4;// bit pattern 000100

what is the value of a&b.

a).63

b).4

c).null

d).0

answer:b

schandu999

Greenhorn

Posts: 9

bala_chocos

Ranch Hand

Posts: 48

kannan_al

Greenhorn

Posts: 4

Vishakha Ahuja

Ranch Hand

Posts: 191

posted 17 years ago

It's very easy to convert a decimal number to binary....

Just divide the given +ve decimal number by 2. Again divide the quotient with 2 repeatedly until you get quotient as zero.

Then collect the remainders (which is either 1 or 0) and line them up in the reverse order i.e. right to left. Add zero's to fill up the remaining places.

Example 1:

2)63(31 2)31(15 2)15(7 2)7(3 2)3(1 2)1(0

62 30 14 6 2 0

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

1 1 1 1 1 1

so: 63 = 0011 1111

Example 2:

2)4(2 2)2(1 2)1(0

4 2 0

-- -- --

0 0 1

so:4 = 0000 0100

Just divide the given +ve decimal number by 2. Again divide the quotient with 2 repeatedly until you get quotient as zero.

Then collect the remainders (which is either 1 or 0) and line them up in the reverse order i.e. right to left. Add zero's to fill up the remaining places.

Example 1:

2)63(31 2)31(15 2)15(7 2)7(3 2)3(1 2)1(0

62 30 14 6 2 0

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

1 1 1 1 1 1

so: 63 = 0011 1111

Example 2:

2)4(2 2)2(1 2)1(0

4 2 0

-- -- --

0 0 1

so:4 = 0000 0100

Vishakha Ahuja

Ranch Hand

Posts: 191

bala_chocos

Ranch Hand

Posts: 48

Rong Chen

Ranch Hand

Posts: 30

softie

Ranch Hand

Posts: 41

Stephanie Grasson

Ranch Hand

Posts: 347

posted 17 years ago

bala_chocos,

vishak is right.

63 decimal = 00111111 binary.

When you convert to binary, the same principles apply as in decimal numbers.

Start with the decimal number 63.

There is nothing in the hundredes position, a 6 in the tens position and a 3 in the ones position.

So we get

(6 * (10^1)) + (3 * (10^0)) =

(6 * 10) + (3 * 1) = 63.

The same logic applies for binary, but now instead of going by powers of 10 we go by powers of 2. Instead of having ones, tens, hundreds, thousands, etc. positions, we have ones, twos, fours, eights, sixteens, etc. positions.

So, getting back to the decimal number 63. In binary we have:

0 in the sixty-fours position

1 in the thirty-twos position

1 in the sixteens position

1 in the eights position

1 in the fours position

1 in the twos position

1 in the ones position

So:

(1 * (2^5)) +

(1 * (2^4)) +

(1 * (2^3)) +

(1 * (2^2)) +

(1 * (2^1)) +

(1 * (2^0)) =

(1 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (1 * 1) = 63;

00111111 binary = 63 decimal

Hope this helps.

Stephanie

vishak is right.

63 decimal = 00111111 binary.

When you convert to binary, the same principles apply as in decimal numbers.

Start with the decimal number 63.

There is nothing in the hundredes position, a 6 in the tens position and a 3 in the ones position.

So we get

(6 * (10^1)) + (3 * (10^0)) =

(6 * 10) + (3 * 1) = 63.

The same logic applies for binary, but now instead of going by powers of 10 we go by powers of 2. Instead of having ones, tens, hundreds, thousands, etc. positions, we have ones, twos, fours, eights, sixteens, etc. positions.

So, getting back to the decimal number 63. In binary we have:

0 in the sixty-fours position

1 in the thirty-twos position

1 in the sixteens position

1 in the eights position

1 in the fours position

1 in the twos position

1 in the ones position

So:

(1 * (2^5)) +

(1 * (2^4)) +

(1 * (2^3)) +

(1 * (2^2)) +

(1 * (2^1)) +

(1 * (2^0)) =

(1 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (1 * 1) = 63;

00111111 binary = 63 decimal

Hope this helps.

Stephanie

It is sorta covered in the JavaRanch Style Guide. |