Bitwise XOR

Page 1 of 1

4 Replies - 5626 Views - Last Post: 14 September 2006 - 07:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=18567&amp;s=0b92e4d59bf3f75acdee01289e75110f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mrgubbala

Reputation: 0
• Posts: 2
• Joined: 13-September 06

Bitwise XOR

Posted 13 September 2006 - 11:52 PM

int counter=0;

if(counter < 10)
counter++;
else
counter=0;

The above code, resets the value to O after reaching 10.
How can we achieve the same using just Bitwise XOR without
"if".

Thanks!
Is This A Good Question/Topic? 0

Replies To: Bitwise XOR

#2 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

Re: Bitwise XOR

Posted 14 September 2006 - 12:18 AM

Xoring the value with itself reset's the value to 0.

#3 mrgubbala

Reputation: 0
• Posts: 2
• Joined: 13-September 06

Re: Bitwise XOR

Posted 14 September 2006 - 12:37 AM

for example,

int counter=0;
int reset=10;

while(1)
{
if(counter^reset)
counter++;
else
counter = counter ^ reset;
}

How can I make it even simplier, may be with a single statement?

#4 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

Re: Bitwise XOR

Posted 14 September 2006 - 01:25 AM

Try this
```count<10?++count:(count^=count);
```

#5 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 184
• Posts: 4,673
• Joined: 26-November 04

Re: Bitwise XOR

Posted 14 September 2006 - 07:15 AM

That's using the ternary conditional operator very nicely.
Great stuff Xing.