Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we just toggle all the bits in a variable using the shortest code?
Posted
Updated 15-Jun-10 21:26pm
Comments
rupeshkp728 16-Jun-10 3:30am    
I guess I asked the question at a wrong place here.
So I have asked the same again at the C message board in codeproject.
OriginalGriff 16-Jun-10 3:41am    
Don't post the same question in multiple places, just because it's been 6 minutes and you haven't got an answer. Most of Europe isn't at work yet!

Use "~" - bitwise not. See
here.[^]
int newInvertedVariable = ~oldVariable;
 
Share this answer
 
XOR operator(^) can be used to toggle a bit.
C
number ^= 1 << x;

Look here for more: Toggling a bit[^]
 
Share this answer
 
You should review the information presented here[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900