Thread: Difference between & and &&

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Difference between & and &&

    I could use some help with some things.whats the difference between,

    if (a & b)
    flag = 0;
    else flag = 1;


    if (a && b)
    flag = 0;
    else flag = 1;

    Also, anyone know a C program which takes a stream of keyboard characters as input, and filters out blanks. cheers people.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    & is a bitwise operation, && is a boolean operation.

    boolean operation is one where the result is true or false, 1 or 0 essentially

    bitwise does the same thing with each bit.

    5 & 4 for instance is
    00000101
    00000100
    -------------
    00000100 = 4
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I though I knew C code, but this boggles my mind!!!!!

    The difference is the first conditional performs a bitwise AND on a and b and provides no real boolean result while the second asks if both a and b are true.

    Filtering blanks from a string (which you will take from your stream or whatever) is as simple as looping though the array of characters and copying everything that isn't a space (or any whitespace if you wish) over to a new string or buffer.
    Sent from my iPadŽ

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by snapsleftguitar
    Also, anyone know a C program which takes a stream of keyboard characters as input, and filters out blanks. cheers people.
    could do that with sed. but you could write one pretty easily too.


    example:

    sed -e 's/[ \t]//g'
    Last edited by FillYourBrain; 08-13-2006 at 11:29 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    a = 1, b = 2
    You get completely opposite answers if you choose & instead of &&

    flag = !(a && b);
    also works.

    > anyone know a C program which takes a stream of keyboard characters as input
    Yeah, a whole bunch of people. But you start by posting your attempt, then we suggest what to do next.
    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.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Nah this guy is going through his homework and trying to cleverly post threads about having problems with this or that just to get answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The difference between & and && or | and ||
    By GSalah in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2011, 09:51 AM
  2. || , |, &&, &
    By jordanguyoflove in forum C Programming
    Replies: 2
    Last Post: 11-29-2008, 03:25 AM
  3. str_replace && str_find
    By q6z4k in forum C Programming
    Replies: 12
    Last Post: 06-01-2008, 04:03 PM
  4. [newb] How is "!(1 && !(0 || 1))" true?
    By eddwills in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 08:19 AM
  5. && or ||
    By chrismax2 in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2005, 04:42 PM