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;
x=x<<31;
printf("x = %d \n",x);

}

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

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.

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).

Crystal said:   1 decade ago
@Sanket.

What is the difference between reversing and changing the sign?

Sanket said:   1 decade ago
Sign can't be reverse, can be change.

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.

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.

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


Post your comments here:

Your comments will be displayed after verification.