posted 15 years ago

In Khalid Mughal's book question no. 3.14 on the page 73 goes like this:

Given a variable x of type int (which may contain a negative value), which are correct ways of doubling the value of x?

Select al valid answers.

(a) x << 1 ;

(b) x = x * 2 ;

(c) x *= 2 ;

(d) x += x ;

(e) x <<= 1 ;

The correct answer given in the book is b, c, d and e.

b, c and d are alright. But will e allso be valid? Suppose, x is a negative number. will x <<= 1 really make it double?

Given a variable x of type int (which may contain a negative value), which are correct ways of doubling the value of x?

Select al valid answers.

(a) x << 1 ;

(b) x = x * 2 ;

(c) x *= 2 ;

(d) x += x ;

(e) x <<= 1 ;

The correct answer given in the book is b, c, d and e.

b, c and d are alright. But will e allso be valid? Suppose, x is a negative number. will x <<= 1 really make it double?

Always say the TRUTH only

posted 15 years ago

try running it in a lil java class -->

output:

output:

**C:\blah>java doubleIt**

negative = -11

positive = 13

negative <<= 1 : -22

positive <<= 1 : 26negative = -11

positive = 13

negative <<= 1 : -22

positive <<= 1 : 26

- Jess

Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep

Prosenjit Banerjee

Ranch Hand

Posts: 105

posted 15 years ago

Mughal Q3.14

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

Thanks Jessica Sant. I din't have the picture of -ve patterns in mind at

that time. I still remember how beautiful the bit pattern of -ve numbers

are! They are realy cleverly made.

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

Thanks Jessica Sant. I din't have the picture of -ve patterns in mind at

that time. I still remember how beautiful the bit pattern of -ve numbers

are! They are realy cleverly made.

Always say the TRUTH only

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |