[C] Binary Counter help

Associate
Joined
14 Jan 2003
Posts
200
Location
NW
Hi all, ive got the following code to generate a binary sequence for a given input size, but what i cant fathom is how to exor each of the outputs i.e 0^0 = 0, 0^1 = 1, 1^1 =0 etc and put them in an array:

int i, n, v, dig[10];
n = 2;
v = (1 << n);

for (i=0; i < v; i++)
{

for (dig=1 << (n-1); dig; dig >>= 1)
printf (" %d", i & dig ? 1 : 0);
printf ("\n");
}
}


maybe im just being simple any ideas?
 
maybe i'm just being simple (which is quite possible, it's been a long day) but i can't quite work out what it is you want to do.

any chance of a slightly more wordy version for the lame and the slow?

:p
 
MrWhippy said:
maybe i'm just being simple (which is quite possible, it's been a long day) but i can't quite work out what it is you want to do.

any chance of a slightly more wordy version for the lame and the slow?

:p

hehe, ok the code when n=2 produces the binary:
00
01
10
11

for n=3
000
001
etc..

What ive been trying to do is to ex-or each of the digits at the output
e.g
for n=3
0^0^0 = 0
0^0^1 = 1 ...

basically producing the ex-or table for a given n, but store the values in an array form to use elsewhere.
 
Which part is causing you problems?

- Generating the binary number sequence from 'n'?
- Putting the numbers into an array?
- XORing the pairs?
 
Last edited:
if it's the "how to XOR" that's got you, i think this info might help.

it covers how to implement binary XOR with AND, OR and NOT logic, and also how to extend XOR to more than 2 terms.

my initial thoughts on how to do it in C veered towards recursion, which depending on your level of experience and/or the bounds of your problem domain, you might want to stay away from. it probably doesn't have to involve recursion though.
 
Last edited:
RobH said:
Which part is causing you problems?

- Generating the binary number sequence from 'n'?
- Putting the numbers into an array?
- XORing the pairs?

The first ones fine it generates the binary numbers, like you say its putting the numbers into an array that im having the most difficulty with :(
 
Back
Top Bottom