• ttlanhil (unregistered)

    #define FLAG_A = 1 << 1 I'm not a C dev, but assuming I remember correctly... not having parens around something in a #define makes my teeth itch - what happens if someone does ~FLAG_A for some reason?

  • TheCPUWizard (unregistered)

    Use C++ - Binary literals since C++14....

  • (nodebb)

    "The C Programming Language" is a really short book.

  • Jaloopa (unregistered)

    Was that spam comment let through because it's funny or because the spam filtering still doesn't work properly?

  • (nodebb)

    I think I know who is next in line for promotion to upper management!

  • Sauron (unregistered)

    The C programming language bytes back.

  • Jason Stringify (unregistered)

    "The (byte) operator tells the compiler that the number is in binary."

    If only there was some easy way to test whether this is true.

  • Steve (unregistered) in reply to ttlanhil

    There's the real WTF right there: using #define. Should have used constexpr - then ~FLAG_A does exactly what it looks like it does.

  • (nodebb) in reply to Steve

    Did Remy edit the page? It has parentheses now.

    constexpr is new in C23.

  • djwong (unregistered)

    #define shouldn’t have an = .

  • djwong (unregistered)

    #define shouldn’t have an = .

  • Tinkle (unregistered) in reply to Steve
    Comment held for moderation.
  • (nodebb)

    I see there's much confusion about the #define with parentheses and equals signs or not.Here's how to do it properly:

    #define FLAG_A ((byte) 10)
    #define FLAG_B ((byte) 1)
    
    byte number = ((FLAG_A) || (FLAG_B))
    

    Plenty of parentheses in case they change operator precedence of = at some point and completely bug free. Not a single one.

  • Simon (unregistered)

    In Ada, 2#11# (the base can be 2 .. 16)

  • Die Kuhe (kein roboter) (unregistered)

    byte number = ((FLAG_A) || (FLAG_B))

    (Usually) No: only one vertical bar (124dec) Also, I hope you know what you mean with "(byte) 10"....

  • Steve (unregistered) in reply to Simon
    Comment held for moderation.
  • Anon E Mus (unregistered) in reply to jeremypnet
    Comment held for moderation.
  • (nodebb)

    I've had more than one newbie trying to loop over an integer with a series of mods and divides to "get the binary representation of the number", when working with bit masks.

  • HK-47 (unregistered)

    This post is bullshit. Decimal 11 = 8+ 2 + 1 meaning that lowest two bits were still correctly set if the goal was 0b11.

    Furthermore, in 2024 you don't advise people to use #define for constants -- if you can #undefine it then it was never a constant to begin with.

    Use a damn const int.

  • (nodebb)

    "C doesn't have a standard way of specifying binary literals"

    The 0b syntax is part of the proposed C23 standard. It's expected to get ISO's "rubber stamp" by the end of this year... So while that's a technically correct statement, you've only got a few weeks in which to keep saying it.

  • (nodebb)

    Sorta.

    The standard that matters is the standard that was in effect when the code was first committed from brain to keyboard.

    Yeah, yeah, yeah, it's nice to go through your entire codebase every few weeks and update any/everything to the latest idioms, add testing and instrumentation, do a zero-based review of the code vs the specs & the user documentation, etc. But nobody does that.

    The real danger is when the standard in use is the one in effect when the dev first learned to program by fiddling around in their parent's basement at home with some very different language from the one now being used.

Leave a comment on “Ripping Away the Mask”

Log In or post as a guest

Replying to comment #:

« Return to Article