• 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

    Thank you Steve, I did not know about constexpr. A nice addition to the toolbox.

    In this instance a simple const would also have worked.

  • (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"....

  • Anon E Mus (unregistered) in reply to jeremypnet

    ... err, that's a logical OR not bitwise...

  • (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.

  • Die Kuhe (kein roboter) (unregistered) in reply to HK-47

    Ehm... sometimes, one wants to correctly set ALL of the bits (because the other ones also may -and often do- have a meaning). Sometimes, instead, a code that just "works by chance" is enough. It depends on the expectations...

  • Caleb Su (unregistered)

    People need to call it "decimal" more often. When people talk about base "10" it only mean base ten if... you're already using base ten...

  • HK-47 (unregistered) in reply to Die Kuhe (kein roboter)

    Yes sometimes you do and othertimes other bits are irrelevant, but whatever the case you don't go and hardcode bitmasks all over the code like that.

    Real WTFs in order of precedence:

    1. Hiring a dev who doesn't know binary.
    2. Not doing code review to catch hardcoded numbers

    Not-A-WTFs:

    1. Whatever author of this "article" thought was a WTF.
  • (nodebb) in reply to Anon E Mus

    Wooooosh!

    The sound of the joke sailing over your head :-)

  • (nodebb) in reply to Die Kuhe (kein roboter)

    If you were replying to me, see my answer to Anon E Mus.

Leave a comment on “Ripping Away the Mask”

Log In or post as a guest

Replying to comment #:

« Return to Article