- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I wonder what the position names are. I'm guessing the code for setting them looks something like:
Admin
I'm sure that even an embedded no file file system would want efficiency. Efficiency, in this case, gouged its eyes out, jammed pencils in its ears, and stuffed cotton up its nose, then crawled with its unmutilated arm and leg to the nearest pool to drown itself.
Admin
Whilst the code seems pretty ghastly, I'm more surprised to see that the encoded positions seem to use straight binary rather than Gray code. It may be that the encoded position does indeed use Gray code and this code performs the conversion in a rather clumsy fashion.
Admin
Bonus points for the octal specifier in the sea of decimal specifiers. Happens to do the right thing, but wait until someone adds a zero before one of the other values!
Admin
To everyone who doesn't get it:
The numbers in the defines aren't in binary.
1001 is one thousand and one, not nine.
Admin
All your base are belong to us!
Well, at least base 2 and base 10. And our grip on 2 isn't that strong.
Admin
Aside from the main WTF, you have this pair of gems:
What really blows my mind is that the author of this travesty apparently knew something about the bitwise operators (as the bitwise AND is used in the initialization) but didn't know how to manipulate a binary value as a binary value.
Ow, ow, ow, ow, my poor, sore brainmeats.
Admin
I'll never again look at the humble computer mouse the same way!
Admin
So the code extracts the four bits from the input number DIO_Input, then converts it to the binary representation in decimal. Finally the data member nCurrentPosition is assigned the actual value.
What is interesting is that DIO_Input already contains the actual value; there isn't even a need to fiddle with the bits! The code above can be replaced by the following two lines (assuming m_sPositionName is an appropriately initialized array):
pWidget->nCurrentPosition = DIO_Input; pWidget->sCurrentPosition = pWidget->m_sPositionName[DIO_Input];
Admin
The real WTF is that the notches do not use Gray code, so it is quite possible that if you turn the wheel slowly, you get 0 -> 1 -> 0 -> 2 -> 3 -> 7 -> 4 for example.
Admin
That is exactly what I was thinking as I read this. So close, yet so very, very, very far away =(
Admin
[frown]
Admin
So, about 500 more posers are going to show us how smart they are that they've heard of Gray codes. My God, you made it through your freshman year of CS. Give yourself a fucking cookie and STFU.
Admin
Featured comment please!
Admin
This is sloppy code. #define POS_0 0 #define POS_1 01 #define POS_2 10 #define POS_3 11 #define POS_4 100 should of course have been #define POS_0 0 #define POS_1 1 #define POS_2 10 #define POS_3 11 #define POS_4 100
Admin
Not even that. It's a #define. So in some places itmay be one thousand and one. In other places it could be nine. Or not.
Admin
What do you want to bet that the first version of this read:
#define POS_0 0000 #define POS_1 0001 #define POS_2 0010 #define POS_3 0011 #define POS_4 0100 #define POS_5 0101 #define POS_6 0110 #define POS_7 0111 #define POS_8 1000 #define POS_9 1001
and that the author had to ask someone why that did not produce the desired results?
Admin
Thats what the line
is for silly!Admin
What they should have done is define a series of macros that convert the Decimal Binary constants into Binary Binary constants and used those in the switch statement. :b
Admin
TRWTF is thinking that TRWTF has anything to do with binary at all here.
Admin
Hey, anybody heard of Gray code? Just checking.
Admin
Bit of a grey herring, if you ask me...
Admin
A real kick in the mouse balls, this is.
CAPTCHA: ingenium - such an ingenious waste of effort...
Admin
Uh, no.
The only way you can get POS_9 to act like anything but "one thousand and one" is to prefix it to change the base:
0x1001 (four thousand and ninety-seven) 01001 (five hundred and thirteen)
Some compilers support 0b as a prefix for binary literals, but that's not standard.
Here's the interesting part that blows your comment away: it's really, really hard to put that prefix on accidentally: "0x POS_9" and "0xPOS_9" won't compile. However, you can do:
#define PREFIX_X(x,y) x##y #define PREFIX(x,y) PREFIX_X(x,y) int x = PREFIX(0x,POS_9); /* x=0x1001 by preprocessor magic */
I would say that's pretty obvious that you're making it hex, though, even if the preprocessor magic is counter intuitive.
So, no -- POS_9 is always "one thousand and one" even though it's a define.
Admin
The real WTF, in case anyone hasn't figured it out yet, is that the programmer didn't convert the pattern of 1's and 0's into a string, and then use the strpos function (or whatever it's called) to extract the relevant information.
Admin
Why doesn't this code use XML? It seems like the next logical step.
Admin
I am reminded of: There are 10 kinds of people in the world: those that understand binary and those that don't
Admin
hah! i love C and think all those who flee from C or C++ are meeps and weepy pants, but I still would like to nominate the author of this hilarity to be "most likely shoot themselves in the foot and a lot of stuff around them in the next 5 minutes".
Admin
I was about to comment on the lack of Gray code, but it was so obvious other people already said it.
Admin
Very high, I suspect, but that's more of a C WTF to be honest.
I'd have far preferred something like a 0o765 syntax (c.f. 0xfed). A 0b1010 syntax might also have merit (for example it would have made for easier reading if the suggested Gray code were used), though if you can't translate hex to binary in your head, then you've no business working with such things.
Admin
Yet you still felt compelled to post about it anyway... Good jorb!
Admin
Admin
oh c'mon - who hasn't done something like this?!
Admin
Everyone's heard of BCD arithmetic. It takes a pure genius to come up with the concept to DCB arithmetic.
Admin
Addendum (2011-06-27 09:05): As was correctly pointed out, the correct code inside of while should be
It was a classic off-by-one in disguise.
Admin
Admin
There are 10 types of people:
Admin
OMG LOL, I never heard that before! Did you just make that up? Ok, I'm done now, sorry
Admin
Admin
For a moment I thought maybe they somehow made the compiler treat integer literals as binary, but then there's this:
Admin
Your estimate is way off.
Admin
You're wrong. There are that many posts, but we're silenced by the forum admins who keep deleting gems like "No, YOUR a racist loser!"
Admin
Admin
Because TRWTF is that we still use a prank language designed by those nutty neckbeards Kernighan and Ritchie.
Seriously: leading 0's change the base? 0x is fine, but there should be 0o or 0c for octal. A straight 0 is just nuts.
Admin
Admin
Better?
Admin
FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU! FUCK YOU!
Admin
According to the code that was posted there should be 11 kinds of people that understand binary, those that do, those that don't and those that think they do.
Admin
Link to FAQ, that's pretty neat.
Looks like they just give you one of the existing literal tokens as a string so they don't have to make the lexer dynamic, which makes sense. Unfortunately, it means you can't do a literal form for sets or lists, and you have to use namespaces to avoid clashes.
Admin