This Q is taken from Barry Boone's mock exam. The author thinks choice d is right, but i think d is wrong because <<= is not shift operator at all.<br /> Also i am confused about the exact meaning of "the high-bit of an int"; what does high-bit mean?<br /> Could anyone give some clarification? Thanks in advance.<br /> Followed is Question 70: <br /> To place a 1 in the high-bit of an int named ref that�s set to 0x00000001, you can write: <br /> Select the one right answer. <br /> a. ref>> 31; b. ref >>= 31; c. ref << 31; d. ref <<= 31; e. Shifts the bits in an integer to the left by the number of bits specified and fills the right-most bit with 1. f. Shifts the bits in an integer to the left by the number of bits specified and fills the right-most bit with 0.

Question 70: To place a 1 in the high-bit of an int named ref that�s set to 0x00000001, you can write: Select the one right answer. a. ref >> 31; b. ref >>= 31; c. ref << 31; d. ref <<= 31; e. Shifts the bits in an integer to the left by the number of bits specified and fills the right-most bit with 1. f. Shifts the bits in an integer to the left by the number of bits specified and fills the right-most bit with 0.

Just as you can say "i += 2;" as shorthand for "i = i + 2;" in Java, you can say "ref <<= 31;" as shorthand for "ref = ref << 31;". An int has thirty-two bits. Here are some ints and their bit representations: -1 = 11111111 11111111 11111111 11111111 +1 = 00000000 00000000 00000000 00000001 +127 = 00000000 00000000 00000000 11111111 +128 = 00000000 00000000 00000001 00000000 -70707 = 11111111 11111110 11101011 11001101

The high bit is the most-significant, or leftmost, bit. For negative ints, the high bit is "1"; for 0 and positive ints, the high bit is "0". The question starts out by telling us that an int named ref is equal to 1. So, in binary representation, ref = 00000000 00000000 00000000 00000001

The question then goes on to ask, how can we make ref's high, or most-significant bit, a "1" instead of a "0"? We can eliminate (a) and (c), for they don't put their results into any variable. That would be like writing "i * 9" in a Java program, without putting the product back into i or some other variable. (b) is a signed right shift, and will shift 31 bits off the right end of ref, replacing the bits on the left with what was originally there (i.e., "0"). If you carry this operation out, though, all you'll be left with is 0. (d) is left shift, and will shift 31 bits off the left end of ref, replacing the bits on the right with zeros. If you perform this operation, the 1 currently in the low bit of ref will graduate to the high bit of ref (it goes from position #32 to position #1 in its 31 shifts to the left). This is the exact scenario the problem is seeking, so (d) is correct. (e) and (f) confuse me, frankly. There's no primitive Java operation that performs what is described, and we're looking at most-significant bit behavior, not least. I hope this helps, Daniel. Art

Daniel Wu
Ranch Hand

Joined: Jun 13, 2001
Posts: 58

posted

0

Art, Thank you very much for the detailed explanation, Art. Now, i am clear. Once again, thanks a lot. Dan

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.