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)

Prakash said:   8 years ago
#include<stdio.h>
int main()
{
int x=1,y;
y=x<<31;
printf("x = %d \n",x);
printf("y = %d \n",y);
}
~
~

Sheereen said:   9 years ago
Actually, in some cases, it is actually possible but in most of the cases, if we try to change the sign using the bitwise operator, the number itself gets changed. Therefore, we CANNOT say that bitwise operator can be used to change the sign of the number.

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.

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.

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

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?

Prakash said:   8 years ago
#include<stdio.h>
int main()
{
int x=1;
x=x<<31;
printf("x = %d \n",x);

}

It will change the sign of x and also changes its value.

Ann said:   1 decade ago
x=1(0001), if we set msb bit in x to high (1001 equals (-7)) it will give a negative number (means sign is changed) but not reversed (-1='1111'in binary).

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.


Post your comments here:

Your comments will be displayed after verification.