C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Yes / No Questions (Q.No. 2)
2.
Bitwise can be used to reverse a sign of a number.
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
16 comments Page 1 of 2.

Naveen said:   8 years ago
Bitwise Cannot change the Sign of the number, the reason is Consider if we are using positive values in a program. If changes the sign the we may get wrong outputs.

If we give a positive number it will give an only positive number. Irrespective of whatever the operation you perform in bitwise, if give a negative number it will negative.
(1)

Wikiok said:   1 decade ago
Correct B answer:
-1*x = ~x+1, so even "~" is a bitwise operator, but "+1" is not. So "B" is correct.

Rafat said:   1 decade ago
I didnt understand your explanation.

Anyways i thought that the sign is decided by the MSB, so bitwise & or | can be used to easily alter the MSB and the sign can be changed... So how B is ans?

Rahul said:   1 decade ago
Answer is wrong we can change sign.

Chaya said:   1 decade ago
Negetive numbers are reperesented according to the 2's complement method.

That's way @Wikiok is right and @Rafat is wrong.

Sreejith said:   1 decade ago
Initially, even I though it will be possible.
For example, you input is i.
(~i)+1 will give you the negative of i.
But in cases where the value are of power 2, it won't give correct values.
So, according to me, the answer should be B itself.

Ritesh Chandora said:   1 decade ago
@all.

i=~i|1;

~ and | both are bitwise operator and this thing can change the sign.

Mehul said:   1 decade ago
@ritesh

i=~i|1; means i=~i;

take example of 2
2=0010 -2= 1110
first take complement of 2(0010) which results in 1101.
now add 1 to it resulting in 1110.
but addition is not a bitwise operator; it is arithmetic operator

Nidhinpradeep said:   1 decade ago
MSB only indicates whether it is +ve or -ve. To find its complement we have to use 2's complement.

Mario said:   1 decade ago
No, Objection !
The bitwise operator can be used to reverse the sign of a certain variable.

Example:
signed char x = 5;

x = x | (1<<7) ; // here we changed the sign of x which is now -5.
//as the sign bit is the msb.


Post your comments here:

Your comments will be displayed after verification.