Let's "switch" to Something Else
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
:doh:
Gryphons Are Awesome! Gryphons Are Awesome!
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
A very smart way of circumventing the restrictions of C--!
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
I did similar in C about twenty years ago, but I could only fit two characters into a character literal. If you'll pardon my rusty C:
char* s ;
// read a line from a file into s
short* i = s ;
switch ( *i ) ...This enabled me to easily parse some reports. I wish I still had the code. :sigh:
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
It's ugly, yes. But it also has the potential to be fast. By loading the three characters and one space into a machine word, it is a single machine instruction to compare each value, and if the compiler spots that - and it damn well should - then it can use a machine-word look up table and index into a machine-word jump table. And you can't get much faster than that! If this is in time-critical code, then it could be justified. Provided it was well commented. If it's in user input handling, then the author needs to meet Mr Firing Squad for being too "clever". :laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
It's ugly, yes. But it also has the potential to be fast. By loading the three characters and one space into a machine word, it is a single machine instruction to compare each value, and if the compiler spots that - and it damn well should - then it can use a machine-word look up table and index into a machine-word jump table. And you can't get much faster than that! If this is in time-critical code, then it could be justified. Provided it was well commented. If it's in user input handling, then the author needs to meet Mr Firing Squad for being too "clever". :laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
-
It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
Perhaps not a meeting with Mr Firing Squad than, but a meeting with Mr Big Stick seems warranted. :laugh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
That's how you handle tags in ICC profiles, for example. It's a perfectly reasonable thing to do.
Tom Delany wrote:
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
Still one of my favorite sigs on CP.
Software Zen:
delete this;
-
I don't think it would, as it's doing the + and shift manually. If it was just
switch(recv[0])
then it would be endian vulnerable. In the absence of language or framework features that allow you to switch on a string, I have no problem with this.You've missed the point ... all multibyte char literals are endian-sensitive. To make it endian-insensitive, it should be
#define VB(a,b,c) (((((a)<<8)+(b))<<8)+(c))
verb = VB(upper[curr->recv[0]], upper[curr->recv[1]], upper[curr->recv[2]]);
switch (verb)
{
case VB('C','M','D'):
... -
That's how you handle tags in ICC profiles, for example. It's a perfectly reasonable thing to do.
Tom Delany wrote:
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
Still one of my favorite sigs on CP.
Software Zen:
delete this;
-
'Sensibility' of a particular approach always depends upon the problem being solved. Making code endian-insensitive is necessary only if you plan on porting it between different endian environments.
Software Zen:
delete this;
-
You've missed the point ... all multibyte char literals are endian-sensitive. To make it endian-insensitive, it should be
#define VB(a,b,c) (((((a)<<8)+(b))<<8)+(c))
verb = VB(upper[curr->recv[0]], upper[curr->recv[1]], upper[curr->recv[2]]);
switch (verb)
{
case VB('C','M','D'):
... -
I have no idea which of the things I wrote are the subject of your "oh really", but all of them are correct. As for the rest, it's a non sequitur. Both *recv and multi-char literals are endian-sensitive, so you appear to have committed a fallacy of affirmation of the consequent ... a rather basic failure of logic. But if you want to go around switching on 4-char literals thinking that it's portable, be my guest ... just don't do it in any code that might affect my life.
-
It's in a thread where it's listening for something from a TCPIP connection (sort of a command channel). It's low-volume, and as such, I don't think the "cleverness" is warranted. And no, it's not commented at all. It's not that I don't understand what he was doing. I just think he was being too clever for his own good. And in the (unlikely?) event that the code is ever ported to a big-endian system, it would be completely broken.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
A wee bit of commmenting would be nice, but other than that I cannot see a problem here. As for porting to big-endian system: First of all, that will never happen. Secondly, there will be more places to fix if it did happen. Thirdly, the code is platform-restricted anyway (to Windows), so there is not a problem...:cool:
-
C++ code. This is either brilliant, or just bloody awful. I'm coming down on the side of "bloody awful": recv in the curr structure below is declared as:
BYTE recv\[64\]; DWORD verb; ... verb = (upper\[curr->recv\[0\]\] << 24) + (upper\[curr->recv\[1\]\] << 16) + (upper\[curr->recv\[2\]\] << 8) + 0x20; switch (verb) { case 'CMD ': ... break; case 'MON ': ... break; case 'END ': ... break; case 'EXT ': ... break; default: ... break; }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.
?? what's wrong with it?
-
You've missed the point ... all multibyte char literals are endian-sensitive. To make it endian-insensitive, it should be
#define VB(a,b,c) (((((a)<<8)+(b))<<8)+(c))
verb = VB(upper[curr->recv[0]], upper[curr->recv[1]], upper[curr->recv[2]]);
switch (verb)
{
case VB('C','M','D'):
...You're not proposing he should use VB are you?
-
A wee bit of commmenting would be nice, but other than that I cannot see a problem here. As for porting to big-endian system: First of all, that will never happen. Secondly, there will be more places to fix if it did happen. Thirdly, the code is platform-restricted anyway (to Windows), so there is not a problem...:cool:
Why is that code platform-restricted to Windows? I've seen DWORDs on many platforms, so there's really nothing here to suggest that.
-
Why is that code platform-restricted to Windows? I've seen DWORDs on many platforms, so there's really nothing here to suggest that.