I've been racking my mathless brain for the last 45 minutes, looked at two books (if Bit Twiddling Hacks counts as a book) all to no avail.
The answer is no doubt really obvious. I'm tired. Very, very tired.
EDIT: Yes, I have googled.
I've been racking my mathless brain for the last 45 minutes, looked at two books (if Bit Twiddling Hacks counts as a book) all to no avail.
The answer is no doubt really obvious. I'm tired. Very, very tired.
EDIT: Yes, I have googled.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
I'm not quite sure what you're asking here. If you want to find whether a number can be stored in 4 bytes, I'd check whether it's less than 2^32.
QuantumPete
Last edited by QuantumPete; 09-07-2007 at 11:23 AM. Reason: made stupid mistake
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
I want to know if it's dead on a 4-byte boundary, like is it equal to 32, 64, 96 etc. I guess I could use a lookup table with a search, but I was hoping there's a way of calculating it.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Hmm, you could mod it with for 4...
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Or I could shoot myself in the FACE for being so stupid. Thank you.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
>I want to know if it's dead on a 4-byte boundary, like is it equal to 32, 64, 96 etc.
Code:if ((num & 0x1f) == 0)
The normal way to check the alignment is to and with alignment-1 and check that it's zero, e.g.If you want to make it generic, you could do:Code:if ((address & 3) == 0) ... // it's aligned else ... // unaligned.This works for all types that have sizes that are 2^n, e.g 2, 4, 8, 16, 32, 64, etc.Code:if ((address & sizeof(type)-1) == 0) ...
Note that the parenthesis around (address & x) are necessary, otherwise the compiler will evaluate 3 == 0 [or whatever] first, then perform the and. If it's false, like in this case, it will be false all over, since X & 0 is always zero, so false. Don't ask me how I remember this, just trust me that if you've made the same mistake enough times, you don't forget quite so easily.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.