Shocked and Stunned (that code exists and does work)

#define READ_ALL		1	/* openfrm: Read all parameters */
#define EXTRA_RECORD		8	/* Reservera plats f|r extra record */

and later on….

  if (prgflag & (READ_ALL+EXTRA_RECORD))
    records++;

Feel free to think about that for a second.

(I have an urge to add this to questions asked in a job interview…)

Tags: , , , ,

12 Responses to “Shocked and Stunned (that code exists and does work)”

  1. Shlomi Noach says:

    It’s not that uncommon to see this… You merely test that there is some shared bits.
    For C programmers this is actually trivial. Somewhat les desired in C++ code.

  2. Think of the addition (not bitwise or)

  3. Shlomi Noach says:

    Assuming (as is the case) the flags are powers of 2 (1,2,4,8,…), it’s the same thing as doing bitwise OR

  4. but is semantically wrong. you’re not doing maths at all… it’s all bitwise operations.

  5. Shlomi Noach says:

    Yes, but it’s a matter of convention. And the above code is not unpopular. So – if it’s conventional…
    I agree bitwise OR is more appropriate.

  6. Xaprb says:

    Wow, that’s a nice one. I don’t suppose any level of lint checking will warn about it.

  7. [...] This post was mentioned on Twitter by IMasterfeed, Zuissi. Zuissi said: MySQL: Shocked and Stunned (that code exists and does work): #define READ_ALL 1 /* openfrm: Read all par.. http://bit.ly/6QVuXu [...]

  8. TimC says:

    Meh. Makes sense to me. But then again, being introduced at any stage of ones life to micro programming can warp ones brain irrepairably.

  9. Tim Little says:

    I could be missing something, and I do agree that if records represents something that is a boolean set, then the mathematical ++ operator is inappropriate, regardless of convention.

    But I saw the code as representing something else actually…
    I THOUGHT it was saying :
    If the extra-record bit is set in the program-flag variable, then increment the number of records.

    What is “records”? Is it a binary/boolean or an int / array or struct?

  10. records here isn’t relevant. it’s about thinking in math when it’s bitwise.

  11. Leon Brooks says:

    Enter the Philistine… (-:

    if (prgflag & (READ_ALL^EXTRA_RECORD))
    records++;

    …or the masochistic Philistine… (-:

    if (prgflag & (READ_ALL¬EXTRA_RECORD))
    records++;

  12. Jobin Augustine says:

    it just works :)
    but ugly code. same behaviour exists in SQL of Drizzle also. sometime back i mailed to discussion group about one accident.
    delete where id-2;
    cleaned up my table.. even if id-2 was not a Boolean type.
    it was a typing mistake and my intention was “id=2″

Leave a Reply