C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - True / False Questions (Q.No. 7)
7.
Left shifting an unsigned int or char by 1 is always equivalent to multiplying it by 2.
True
False
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 1 of 2.

Coder said:   1 decade ago
I don't think so.

X=10100000 now left shift by.

X<<1 this is not actually multiplication by 2.

Aashish D. said:   1 decade ago
X=10100000 represents the number 160,

If we use left shifting operator,
i.e. X<<1, it gives X=101000000,

That means it represents 320 in decimal number.

Hope you understand. Correct me If I am wrong ?

Sanchana said:   1 decade ago
If we take a simple eg x=0001 is representing the no 1. If it is left shifted by 1 means x=0010 i.e. 2. Similarly if we check for all numbers we will get the answer.

Pingu said:   1 decade ago
I think what coder is trying to say is that shifting 10100000 by one bit to the left will give 01000000 because the result should be contained in one byte, like in case of char. So left shifting an unsigned integer with the leftmost bit on won't always give you multiplication by 2.

Puneet Kumar Garg said:   1 decade ago
Actually x=10100000=160.

Then x<<1 x=101000000=320 (actually result will be evaluated for whole 9 bits).

So it is right.

Anonymous said:   1 decade ago
@Puneet Kumar Garg, consider:

unsigned long long k = 1 << (sizeof(k) * CHAR_BIT - 1);
assert(k * 2 != k << 1);

k * 2 will overflow (undefined behavior) while k << 1 will be 0.

Abhi said:   9 years ago
Not necessarily. If the number is larger than (max_range/2) , then overflow should occur. For example if unsigned no has max range 65536, then any no greater than 65536/2 would lead to overflow if shift operation is done.

Lolcode said:   9 years ago
it's not a 4-bit computer, it's 32/64. So you can't say 4 = 0100 only, it actually is 0000 0000 0000 0000 0000 0000 0000 0100 now left shift.

discussion over -_-

Nileshy said:   8 years ago
I think the answer should be 'false'.

Rahul said:   8 years ago
0<<1 is it divisible by 2?
0 left shifted is this equivalent to 2*0?


Post your comments here:

Your comments will be displayed after verification.