+ Post New Thread
Results 1 to 11 of 11
  1. #1
    Advanced Member level 5
    Points: 32,919, Level: 44
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    6,848
    Helped
    496 / 496
    Points
    32,919
    Level
    44

    Are these two lines of C code for PIC16F1947 the same?

    Hello,
    Our software engineer tells us that the following two lines are the same in every way. Is this true? Do they both execute the same?
    (as you can see , one has "!=" and the other has "=!".
    The C compiler is the free one that comes with microchip's MPLAB software.



    if(eepromWriteCount != BACKUP_1_eepromWriteCount)
    if(eepromWriteCount =! BACKUP_1_eepromWriteCount)

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 260,190, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,437
    Helped
    13824 / 13824
    Points
    260,190
    Level
    100

    Re: Are these two lines of C code for PIC16F1947 the same?

    I think you can answer the question yourself applying average C language knowledge.

    The first is a pure comparison, the second is an assignment changing the value of eepromWriteCount.

    Even if eepromWriteCount is discarded, it's a different if expression argument. First is true if both variables are unequal, the second if BACKUP_1_eepromWriteCount is zero.



  3. #3
    Super Moderator
    Points: 81,377, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,337
    Helped
    4454 / 4454
    Points
    81,377
    Level
    69

    Re: Are these two lines of C code for PIC16F1947 the same?

    Agreed.... shoot the software engineer.....

    != means "if not equal do the following" (a comparison)
    =! means "make it equal to the following if it isn't true" (assigns a value to eepromWriteCount of zero or one)

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  4. #4
    Super Moderator
    Points: 29,619, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,859
    Helped
    1114 / 1114
    Points
    29,619
    Level
    41
    Blog Entries
    9

    Re: Are these two lines of C code for PIC16F1947 the same?

    Anyway, an assignment within a conditional expression is something totally not recommended, and would be expected the compiler issue a warning in this case, since it is not a standard implementation, which could be interpreted differently from one compiler to another.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  5. #5
    Super Moderator
    Points: 260,190, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,437
    Helped
    13824 / 13824
    Points
    260,190
    Level
    100

    Re: Are these two lines of C code for PIC16F1947 the same?

    Anyway, an assignment within a conditional expression is something totally not recommended, and would be expected the compiler issue a warning in this case, since it is not a standard implementation, which could be interpreted differently from one compiler to another.
    I almost agree, except for a potentially different interpretation.

    IMHO, the C language specification is crystal clear in this relation: an assignment is an assignment, the if statement is evaluating the assigned value. The compiler should issue a warning though, because the assignment is possible inadvertent.

    Writing if (a = b) instead of if (a == b) is a pretty popular fault.



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 5
    Points: 32,919, Level: 44
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    6,848
    Helped
    496 / 496
    Points
    32,919
    Level
    44

    Re: Are these two lines of C code for PIC16F1947 the same?

    thanks, i cant find where the offlicial meaning of "=!" is given for C?

    Are you sure that "=!" means anything at all?

    https://en.wikipedia.org/wiki/Operat..._C_and_C%2B%2B



  7. #7
    Advanced Member level 5
    Points: 32,919, Level: 44
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    6,848
    Helped
    496 / 496
    Points
    32,919
    Level
    44

    Re: Are these two lines of C code for PIC16F1947 the same?

    ok sorry, i see, it is an assignment as you all kindly say.



  8. #8
    Super Moderator
    Points: 81,377, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,337
    Helped
    4454 / 4454
    Points
    81,377
    Level
    69

    Re: Are these two lines of C code for PIC16F1947 the same?

    I wasn't suggesting it was good programming practice to use it at all, especially in an 'if' statement.

    "if(a == !b)" reads as "if a is equal to the inverted state of b".
    "if(a != b)" reads as "if a is not equal to b".
    "if(a =! b) reads as "assign the value to a of the inverted state of b"., the 'if' is ambiguous as no query is made inside the braces.

    However in the first post the implication is that both a and b are numeric values rather than boolean. I think the line is intended to ask if the working and backup values are different so "!=" is the operator to use.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisement

        
       

  9. #9
    Super Moderator
    Points: 260,190, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,437
    Helped
    13824 / 13824
    Points
    260,190
    Level
    100

    Re: Are these two lines of C code for PIC16F1947 the same?

    "if(a =! b) reads as "assign the value to a of the inverted state of b"., the 'if' is ambiguous as no query is made inside the braces.
    IMHO it's bad programming style, but not ambiguous. It's just a shortcut of
    Code C - [expand]
    1
    2
    
    a = !b;
    if (a) ..



  10. #10
    Advanced Member level 5
    Points: 32,919, Level: 44
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    6,848
    Helped
    496 / 496
    Points
    32,919
    Level
    44

    Re: Are these two lines of C code for PIC16F1947 the same?

    However in the first post the implication is that both a and b are numeric values rather than boolean. I think the line is intended to ask if the working and backup values are different so "!=" is the operator to use.
    Thanks yes the software guy now wants to use "!="

    The following of his code indeed shows that they are not boolean types, so yes i suppose it would be a mystery how the compiler would deal with this "=!"..


    // EEPROM VAR (part of eeprom where data change often)

    volatile int32_t eepromWriteCount; // number of writes for this block

    volatile int32_t BACKUP_1_eepromWriteCount;



  11. #11
    Super Moderator
    Points: 260,190, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,437
    Helped
    13824 / 13824
    Points
    260,190
    Level
    100

    Re: Are these two lines of C code for PIC16F1947 the same?

    The following of his code indeed shows that they are not boolean types, so yes i suppose it would be a mystery how the compiler would deal with this "=!"..
    Again, it's not a mystery, the C standard is crystal clear, both about the comparison "!=" and the logical not "!" applied to numerical types. Most of this thread discussion can be safed by reading a C text book like Kernighan & Ritchie.

    As for the "!" operation, the result is 1 if the argument is equal to zero else 0.



--[[ ]]--