Thread: The difference between & and && or | and ||

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    The difference between & and && or | and ||

    Please can anybody tell me the difference between & and && , | and || with example .

    thank you.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    && is logical and.
    true && true is true and everything else is false.
    & is bitwise and.
    (10 & 8) = 8
    It can be explained as binary:
    Code:
     00000110
    &00000100
    =00000100
    and
    (83 & 15) = 3
    Code:
     00101011
    &00000111
    =00000011
    And the same thing with bitwise or (| operator):
    (83 | 15) = 95
    Code:
     00101011
    |00000111
    =00101111
    That means every bit is compared.

    http://www.cprogramming.com/tutorial...operators.html
    Read about other bitwise operators.
    Last edited by maxorator; 11-08-2006 at 03:18 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Logical operators work with two values - true (1) and false (0), and returns a boolen value.
    Bitwise operations sort of perform the logical operation for each pair of bits in a char or int for example.

    The results needn't be "similar" at all. See this:
    Code:
    #include <iostream>
    
    int main()
    {
        unsigned int a = 16, b = 15;
        //display bool values as true/false
        std::cout << std::boolalpha; 
        std::cout << "Logical " << a << "&&" << b <<
            ": " << (a && b) << std::endl <<
            "Bitwise " << a << "&" << b <<
            " : " << (a & b) << std::endl;
        std::cin.get();
    }

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    thank you all , its now clear to me

    but in a condition IF , we should use && and || so as to result a logical value , true ?
    Last edited by GSalah; 11-09-2006 at 04:19 AM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For most common tasks, yes.

    Unless you specifically want to test if a bitwise operation returns 0 or non-zero.
    For example you can take advantage of the fact that n&(n-1) equals 0 only if n is a power of 2 to do something like that:
    Code:
    #include <iostream>
    
    int main()
    {
        for (unsigned int i = 1; i <= 64; i++) {
            if (i&(i-1)) {
                std::cout << i << ": not power of 2" << std::endl;
            }
            else {
                std::cout << i << ": power of 2" << std::endl;
            }
        }
        std::cin.get();
    }

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    yes , its now completely clear to me . thank you anon

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    1
    With Bitwise operation Carry bit generated but it don't add up with next bit, for ex: 0x0010 | 0x1010 = 0x1010
    here 2nd bit addition 1+1 = 1 and carry =1, which doesn't get added with 3rd bit.

    So its operation fairly with individual bit.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Acquaint yourself with the rules before digging up threads many years old.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. || , |, &&, &
    By jordanguyoflove in forum C Programming
    Replies: 2
    Last Post: 11-29-2008, 03:25 AM
  2. && and || operator confusion
    By rohit_second in forum C Programming
    Replies: 8
    Last Post: 09-02-2008, 12:10 AM
  3. && or ||
    By chrismax2 in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2005, 04:42 PM
  4. Confused with specific && and || logic
    By the dead tree in forum Windows Programming
    Replies: 2
    Last Post: 04-23-2004, 01:03 PM
  5. AND OR NOT logical && || !
    By sballew in forum C Programming
    Replies: 2
    Last Post: 09-03-2001, 03:06 PM