Miscellaneous

Exclusive OR

The bitwise exclusive or operation may be simulated by a combination of other bitwise operations.

    /* a XOR b == NOT(a AND b) AND (a OR b)                    */

    /* a and b have every combination of opposing bits         */

    unsigned char a = 0x03;               /* binary 0000 0011  */
    unsigned char b = 0x05;               /* binary 0000 0101  */

    /* note: == has higher precedence than the bitwise ops     */

    if ((a ^ b) == (~(a & b) & (a | b)))
        printf("a XOR b == NOT(a AND b) AND (a OR b)\n");

Logical Boolean

A logical expression evaluates to zero when it is false, and one when it is true.

    /* Assign a boolean value to an index                      */

    int i;
    char c = 'x', d = 'y', e = 'z';
    char *msg[2] = {"false", "true"};

    /* note that no parentheses are needed                     */

    i = c == 'a' && d == 'b' || e == 'c';

    printf("This relation is %s\n", msg[i]);         /* false  */

    i = c == 'a' && d == 'b' || e == 'z';

    printf("This relation is %s\n", msg[i]);         /* true   */

Copyright © 2018 The Stevens Computing Services Company, Inc.  All rights reserved.