The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bitwise and if Puntino High Level Programming 3 04-28-2008 10:22 AM
Bitwise negation dLloydm Shell Programming and Scripting 3 05-17-2007 10:57 AM
bitwise operators areef4u UNIX for Advanced & Expert Users 9 08-04-2006 05:39 AM
Bit-fields and Bitwise operators amatsaka High Level Programming 3 04-23-2002 05:13 AM
Resetting WTMP? cuppjr UNIX for Dummies Questions & Answers 4 02-22-2001 09:58 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-13-2006
Registered User
 

Join Date: Feb 2005
Posts: 7
Question resetting counter using bitwise XOR

Hi !
How to reset a variable to 0 after a reset value, say 10 using bitwise
XOR.

For example,

int cnt=0;

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

How can we achieve this by using XOR only.

thanks,
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-14-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Remember that any number, when bitwise XORed with itself will become 0.

Code:
if(cnt<10) {
   cnt++;
   fprintf(stdout,"cnt: %d\n",cnt);
}
else {
   cnt=cnt^cnt;
}
If you put this in a while(1), you'll get an infinite loop.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0