Thread: && and & - please help

  1. #1
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Question && and & - please help

    Hello,

    the following code works well. Inadvertently I wrote '&' instead of '&&'. The results where still the same. Why did it still work?

    Code:
    char array1[CONST2];
    char input_buffer[CONST2];
    unsigned short int counter1 = 0;
    ...
    for ( i = 0 ; (i < CONST2) && (input_buffer[i] != '\n') ; i++ )
    {
           array1[counter1] = input_buffer[i];
           counter1++;
    }
    ...
    Thank you for your help.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    It works because the statements:
    (i < CONST2)
    (input_buffer[i] != '\n')
    will give you a 1 or a 0

    This gives you the following truth table:
    Code:
             &    &&
    0 0    0     0
    0 1    0     0
    1 0    0     0
    1 1    1     1
    But if you were to have values that are not necessarily only 1 and 0, then your results will differ.

    For example:
    1 && 4 = 1
    1 & 4 = 0
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Smile

    Thank you very much, Cshot, for your fast answer.
    I guess, I will have to learn more about '&' and '&&'. But for now I am glad, that my little typing error did not make things worse!

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > But for now I am glad, that my little typing error did not make things worse!

    Better an error makes your program explode with fireworks and error messages than not showing you a subtle problem - it's way easier to fix that way.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > than not showing you a subtle problem
    I wonder who else can spot the other subtle problem of using & instead of &&
    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 Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Govtcheez
    > Better an error makes your program explode with fireworks and error messages than not showing you a subtle problem - it's way easier to fix that way.
    I wish, it would have been that way. I am glad, that I know now why the program works because the results of my program have already been used for scientific research. Finally, I can sleep well again, now that I now the results are correct.

    @Salem:
    Oh no, another problem? Please, tell me!
    Last edited by Sargnagel; 09-03-2002 at 01:56 PM.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Salem
    > than not showing you a subtle problem
    I wonder who else can spot the other subtle problem of using & instead of &&
    A logical and (&&) will evaluate to true if both sides are nonzero...

    A bitwise and (&) will only evaluate to true if both sides have at least one bit set at the same position

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    	int a = 1,
    		b = 2;
    		
    	cout << (a && b) << endl;// 1
    	cout << (a & b) << endl;// 0
    	
    	return 0;
    }
    Both a and b are non zero. The first evaluates to true (fair enough...both sides are non zero). The second evaluates to false even though both sides are still nonzero

    0001 && 0010 = 1

    0001 & 0010 = 0
    Last edited by Fordy; 09-03-2002 at 02:30 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Fordy, that's more or less what Cshot said

    <yoda>no, there is another</yoda>
    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.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Salem
    Fordy, that's more or less what Cshot said

    <yoda>no, there is another</yoda>
    DOH....

    ::must learn to read one of these days::


  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    OK, the other problem

    (i < CONST2) && (input_buffer[i] != '\n')
    vs.
    (i < CONST2) & (input_buffer[i] != '\n')

    The first is correct because it is guaranteed to be evaluated in left to right order. If the left hand side is false, the right hand side is NOT evaluated.

    However in the second case, both sides of the & are always evaluated, which means that when say i == CONST2, the left side will be false (which we want), but the right hand side goes on to reference an index outside the array (this is not good).
    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.

  11. #11
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Thank you for this vital information, Salem!!!
    I will again take a deep look at the source code and especially at the results of some calculations.
    But I think (hope) that the case i=CONST2 never happens because '\n' is always found before ... except for the EOF.

Popular pages Recent additions subscribe to a feed