# Bitwise XOR

Page 1 of 1

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

### #1 mrgubbala

• New D.I.C Head

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

• D.I.C Addict

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.
Was This Post Helpful? 0

### #3 mrgubbala

• New D.I.C Head

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?
Was This Post Helpful? 0

### #4 Xing

• D.I.C Addict

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);
```

Was This Post Helpful? 0

### #5 born2c0de

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

Reputation: 187
• 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.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }