| and &

This is a discussion on | and & within the C Programming forums, part of the General Programming Boards category; What do these operator do exactly int m=5; int n=4; k= m&n; or K = m|n; thx...

  1. #1
    Unregistered
    Guest

    | and &

    What do these operator do exactly

    int m=5;
    int n=4;

    k= m&n;

    or

    K = m|n;
    thx

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Those are bitwise binary operators.

    The operator | is the logical or. Definition: x or y is 1 iff one of them or both are 1.

    The operator & is the logical and. Definition: x and y is 1 iff both are 1.

  3. #3
    Unregistered
    Guest
    Thanx

    But could you tell me that in lamance nubee terms

    for m=5 and n=4

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    In binary:

    m = 0101
    n = 0100

    Apply &- and |-operation using the definition:

    Code:
    m n m|n
    0 0  0
    1 1  1
    0 0  0
    1 0  1
    
    m n m&n
    0 0  0
    1 1  1
    0 0  0
    1 0  0
    So in decimal:

    m | n = 5
    m & n = 4

    There are some more bitwise operators, read this thread for more info:
    http://www.cprogramming.com/cboard/s...wise+operators
    Last edited by Shiro; 05-25-2002 at 08:04 AM.

  5. #5
    Unregistered
    Guest
    Thanx really appreciated. Got a c exam 2morrow so just going through a few things

    Thanx again shaun

  6. #6
    Unregistered
    Guest

    Talking

    Sorry to bug you again but what if they where changed

    from m||n

    and m&&n

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The operators || and && are relational operators and no binary operators.

    (A || B) is 1 if (A > 0) or (B > 0), else 0.
    (A && B) is 1 if (A > 0) and (B > 0), else 0.

    Note that A and B are not necessarily bits, they can also be of type int, char etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21