Jak zrobić bit wise XOR w C

Próbuję dostać się do programowania w C, i mam problem z napisaniem bitowej funkcji XOR tylko z operatorami ~ i &. Przykład: bitXor(4, 5) = 1. Jak mogę to osiągnąć?

Póki co mam to:

int bitXor(int x, int y) {

    return z;
}
 12
Author: Peter Mortensen, 2012-09-11

6 answers

Pomyślmy o tym. Co robi XOR?

x   y    XOR
------------
0   0     0
1   0     1
0   1     1
1   1     0

Więc jak przekształcić to w funkcję? Pomyślmy o AND, oraz odwrotnej kolejności AND (~x& ~ y) (tak się składa, że NOR):

              (~x&~y)
 x   y   AND    NOR   
 ---------------------
 0 & 0  = 0      1    
 1 & 0  = 0      0 
 0 & 1  = 0      0
 1 & 1  = 1      0

Patrząc na te dwa wyjścia, jest całkiem blisko, wszystko co musimy zrobić, to tylko ani dwa poprzednie wyjścia (x I y) (x ani y) i mamy rozwiązanie!

  (a)       (b)    ( a NOR b )
x AND y   x NOR y    ~a & ~b
-------------------------------
   0         1          0
   0         0          1
   0         0          1
   1         0          0

Teraz napisz to:

a = ( x & y )
b = ( ~x & ~y )
XOR'd result = (~a & ~b)
BINGO! Teraz po prostu zapisz to do funkcji
int bitXor(int x, int y) 
{
    int a = x & y;
    int b = ~x & ~y;
    int z = ~a & ~b;
    return z;
}     
 35
Author: Mike,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-13 02:41:58

Using NAND logic:

int bitNand(int x, int y)
{
    return ~ (x & y);
}

int bitXor(int x, int y)
{
    return bitNand( bitNand(x, bitNand(x, y)),
                    bitNand(y, bitNand(x, y)) );
}

Lub:

int bitXor(int x, int y)
{
    return ~( (x & y) | (~x & ~y) );
}

Lub:

int bitXor(int x, int y)
{
    return (x & ~y) | (~x & y);
}

Oczywiście, że tak jest łatwiej:

int bitXor(int x, int y)
{
    return x ^ y;
}
 26
Author: David Schwartz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-22 19:04:43

Łatwo zauważyć, że

x ^ y = (x | y) & ~(x & y)

Pozostaje więc wyrazić {[2] } tylko & i ~. Prawa De Morgana mówią nam

x | y = ~(~x & ~y)
 8
Author: Daniel Fischer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-11 18:36:50

Chcę, żeby pisało tylko z ~ i &

To się liczy dla bramek NAND, prawda? Po przestudiowaniu tego schematu obwodu:

int z = ~ ((~(a & ~(a & b)) & (~(b & ~(a & b)));

To samo dotyczy również nie-bitowego, tj. logicznego, po prostu zastąp ! zamiast ~.

 0
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-11 18:42:39

Możesz wykonać bitową operację XOR w c używając operatora ^.

 0
Author: MVP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-21 10:35:32
int xorro(a, b)
{
    if (a == b)
        return 0;
    return 1; 
}
 -3
Author: vaibhav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-18 20:57:37