& operator

This is a discussion on & operator within the C Programming forums, part of the General Programming Boards category; How do you perform the binary operation '&' on two ints? and what does it do? I thought '&' was ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    & operator

    How do you perform the binary operation '&' on
    two ints? and what does it do?

    I thought '&' was '&&' and used for logical operations...

    Any help is appreciated

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > what does it do?

    All numbers are represented in binary, right?

    So, let's say i == 4 and j == 6.

    4 == 100 in binary
    6 == 110 in binary

    4 & 6 == 100 == 4

    Whichever bits the two values both have set will be set in the answer

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The &-operator is the bitwise logical AND.

    Assume A and B are integers, than C, which is (A AND B) can be calculated as follows:

    C = A & B

    The result depends on the values of A and B. Bitwise:

    1 & 1 = 1
    1 & 0 = 0
    0 & 1 = 0
    0 & 0 = 0

    Example of bitwise AND for a:

    A = 1111 0101
    B = 0100 1001
    --------------------
    C = 0100 0001

    && is a logical operation which can be used in for example if-constructions. Assume E and F are expressions. Then

    Code:
    if (E == TRUE && F == TRUE)
    {
    }
    If and only if E evaluates to TRUE and F evaluates to TRUE, then the code between the brackets will be executed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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