You are not logged in.
Pages: 1
Hello there! I have a problem for all you thinkers!
I have three words of 3 bits. Each word acts like a flag, so one bit is 1 and the other 2 are 0.
Eg:
001
010
100
I have the possibility to use a negated logic, having one 0 and two 1s... Check later if that helps you!
So, i have those three words, and with simple combinatorial operations, and the same function for all three cases, I want the following to happen:
1. If the three words are all the same, I get 000.
100
100
100
___
000
2. If the three words are all different, I get 000.
001
010
100
___
000
3. Else, I get anything that is not 000. (001, 010, 011, 100, 101, 110, 111)...
That is, if there are two equal words and one different, I get "false", which is 1 somewhere....
100
100
010
___
>=1
You can use AND, OR, XOR, NOT (NAND, NOR, XNOR) in the result or any of the arguments, as you wish. The function just has to be the same for all three cases.
Can you help me?
(Please post any questions if there are!)
EDIT:
To put it in simple words, all I want is to distinguish cases 1 and 2 from case 3, with simple combinatorial operations.
Last edited by nDray (2008-04-07 22:39:50)
Offline
is this what you're looking for?
#include <stdio.h>
int f(int a, int b, int c)
{
return ((a|b|c)^(a^b^c));
}
int main(int argc, char *argv[])
{
int ai[3], i;
for(i = 0; i < 3; i++)
ai[i] = 1;
for(;;)
{
if(f(ai[0],ai[1],ai[2]) == 0)
printf("*** 0 found: ");
printf("%i,%i,%i\n",ai[0],ai[1],ai[2]);
i = 0;
ai[0] <<= 1;
while(ai[i] > 4)
{
ai[i] = 1;
if(++i >= 3)
return 0;
ai[i] <<= 1;
}
}
return 0;
}
Offline
If you have
int f(int a, int b, int c)
{
return ((a|b|c)^(a^b^c));
}
with input (100,100,100), you get:
(100 | 100 | 100) ^ (100 ^ 100 ^ 100) = 100 ^ 000 = 100, this isn't as should be, is it?
Offline
let me have a look later... What I got was close looking, having to negate the XORs and use AND instead of XOR between the terms...
(- ( A ^ B ^ C )) & ( A | B | C )
EDIT:
yeah, that can't be because with 100 100 100 I want the result to be 0....
I'm actually looking now to shrink this to 2 bit words ignoring 00 ("don't care") and using 01, 10 and 11 as the new words... If the logic is as simple as for 3 bit, it would be quite helpful...
I have karnaugh map minimizer calculating that for 2 variables, but it seems frozen...
e_tank, what I want with this is really make it simple by using simple combinatorics so that I can output the result without many cycles and such...
Last edited by nDray (2008-04-08 10:07:51)
Offline
If you have
int f(int a, int b, int c) { return ((a|b|c)^(a^b^c)); }
with input (100,100,100), you get:
(100 | 100 | 100) ^ (100 ^ 100 ^ 100) = 100 ^ 000 = 100, this isn't as should be, is it?
given binary numbers 100,100,100 (4,4,4) the function returns 0 not 100, i included a program that shows this as well (it does returns 0 for 4,4,4). the or operator sets bits, the xor flips them so
(100 | 100 | 100) ^ (100 ^ 100 ^ 100) = (100 | 100) ^ (000 ^ 100) = 100 ^ 100 = 0
Offline
Ramses de Norre wrote:If you have
int f(int a, int b, int c) { return ((a|b|c)^(a^b^c)); }
with input (100,100,100), you get:
(100 | 100 | 100) ^ (100 ^ 100 ^ 100) = 100 ^ 000 = 100, this isn't as should be, is it?given binary numbers 100,100,100 (4,4,4) the function returns 0 not 100, i included a program that shows this as well (it does returns 0 for 4,4,4). the or operator sets bits, the xor flips them so
(100 | 100 | 100) ^ (100 ^ 100 ^ 100) = (100 | 100) ^ (000 ^ 100) = 100 ^ 100 = 0
Ow I'm sorry, I was wrong Your function might be what the OP is after then.
Offline
How about that?
#include <stdio.h>
unsigned char f(unsigned char a, unsigned char b, unsigned char c)
{
return ((~(a & b & c)) & ((a & b) | (a & c) | (b & c)));
}
int main(int argc, char **argv)
{
unsigned char ret;
unsigned char a = 0x04, b = 0x01, c = 0x01; // sample values
ret = f(a & 0x1, b &0x1, c & 0x1) | f( (a >> 1) & 0x1, (b >> 1) & 0x1, (c>>1) & 0x1) | f((a >> 2) & 0x1, (b >> 2) & 0x1, (c >> 2) & 0x1);
printf("0x%x\n", ret);
}
Offline
Pages: 1