Journal tomhudson's Journal: Use a union instead of bit-shifts and bitwise ANDs ... 18
I was looking at the code that does IP address manipulation, and said to myself "These macros suck!"
4 different macros, one to extract each dot-quad digit, from the 32-bit unsigned raw value.
I came up with a quicker, simpler, and (I think) better way, using a union. I gave a quick walk-through to a co-worker today and he likes it
I've posted the description and code here
The code is simple - one struct, one union. The best part is that, unlike the macros, that used shifts and ANDS, this is reversible - assign a 32-bit value, extract the 4 bytes; assign the 4 bytes, extract the 32-bit value.
Its written for little-endian cpus (little end | lowest byte comes first in ram - eg x86) - a simple #ifdef can handle different architectures, if you're ambitious. Post your comments, rate it, tell the world I'm an ID-10-T
cool idea... (Score:2)
kingtroll@...
ventriloquist@... (just don't offer to shake hands after posting...)
ogregrunt@...
imunderstood@...
opinionsbelong@...
fartback@...
youbelong@...
noplaceforwimps@...
sensitivitiescrushed@...
battlescarred@...
If nothing else, the above should cue up a myriad of other ideas...
I'd like roaringback@... myself.
Re: (Score:2)
shrek (Score:2)
(capcha irony:"quaint")
Re: (Score:2)
Mind you, so is pir8bay.com.
Ah unions... (Score:1)
It's like good coffee vs. brown bitter water that's passed off as coffee.
ARINC429 and even 1553 messages just flow out info so much smoother when picked apart in unions.
ADA kind of has unions, except that ADA's meaning of existence is to make such transformations take at least 5 step
Re: (Score:2)
Surprisingly enough, a lot of programmers sort of "glossed over" unions. Not seeing any immediate use for them, they do things the hard way.
A good example is from c++ - most Point classes have a method - move - that changes the x and y coordinates. Much easier to just pack it into a union (for example, 2 shorts), and add or subtract an int from it that itself is composed of 2 shorts, for the changed directions. Expand this example to a Rect, and you can set all 4 coordinates with 1 8-byte write.
You cou
Good idea... (Score:2)
Oh, and for an email address, you might try billygoatse@... or billygoatse.gruff@...
Re: (Score:2)
Of course, if you're going to get that groovy, why not cut to assembly? That's the supreme method for pissing off the higher-level language snobs.
Re: (Score:2)
Of course, finding an employer that wants that sort of stuff on a day-to-day basis ... that's another story, unless they're a hardware manufacturer.
Re: (Score:2)
Re: (Score:2)
That gave me a headache for a couple of minutes :-) I'm a big fan of fall-through case statements, so yes, I like that one. Thanks.
Re: (Score:2)
Re: (Score:2)
I'm glad you like it. I figure it might or might not save cpu cycles, but it will definitely save grey matter cycles, and they're a LOT more expensive. Its not like I can just swallow another stick of ram and exchange my "twin lobes/dual core" for a quaddie - at least not yet :-)
What gets me is that this is SO simple, and yet I see the bit and byte-slicing macros in c code everywhere.
I figure that I'll use trolltalk.com as a sort of "repository" for tricks like that (in the various programming language
hey nifty (Score:2)
no need to so hard on yourself, you caught something ugly, made it pretty and then were nice enough to share it.
an idiot would have the mindset oh well it works, so what if its convooluted and sucks, leave it to the next person to deal with. i would more than likely be that person since i did a lot of maintenance to get promotions.
Umm...that looks very familiar (Score:2)
From winsock.h:
Re: (Score:2)
Nope, no plagarism involved, and normally I don't do Windows (bring a wheelbarrow of money and we'll talk ;-).
However, as you point out, your post is from winsock.h, which had to keep combatibility with 16-bit code - hence the two shorts. On 16-bit platforms, a short == an int; on 32-bit platforms, a long == an int, so happily, if you declare the whole thing as a long, it works for both.
Time to rework the standard names ... when 128-bit cpus become mainstream in another 15 years, its going to be awkwar
Re: (Score:2)
BTW, I find unions quite useful when dealing with messaging protocols.
Re: (Score:2)
I know you wouldn't ever do that - I just wanted to make it clear to anyone else reading the thread that there was no hanky-panky :-)
I don't know which is more under-used for "weird stuff" - unions or the comma operator ...