|
|
Subject:
24-bit unsigned int in
Category: Computers > Programming Asked by: tomfinnigan-ga List Price: $12.00 |
Posted:
17 Mar 2005 12:39 PST
Expires: 16 Apr 2005 13:39 PDT Question ID: 496319 |
I'm trying to make a unsigned integer type in c/c++ that only takes 24 bits. A piece of sample code looks like this: ======================= #include <stdio.h> #pragma pack(push, 1) struct test { unsigned int data : 24; }; #pragma pack(pop) int main( int, char** ) { printf( "Sizeof(test) = %d\n", sizeof( test ) ); return 0; } ======================= Using gcc, the output is "Sizeof(test) = 3" Using msvc7.1 and 6.0, the output is "Sizeof(test) = 4" How can I get an integer that is 24bits, that I can treat like an integer? I can create a struct of 3 chars, which is 3 bytes, but that doesn't meet my needs. Basically, I want similar results out of msvc7.1 as I get out of gcc. Specifically, I need something that can be incremented, converted to and from a 4-byte int, used as an index into an array, and only takes 3 bytes of storage. On msdn, they have some good information at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcepbguide5/html/wce50conworkingwithpackingstructures.asp but they don't explain how to do what I need to. Thanks. | |
| |
|
|
There is no answer at this time. |
|
Subject:
Re: 24-bit unsigned int in
From: mathtalk-ga on 17 Mar 2005 20:06 PST |
It seems to me the simplest implementation would be to define "overloaded" operators on a 3-byte structure by using a (4 byte/32-bit) integer that the 3 bytes define (in least significant positions) to do every required operation, then repack the least significant three bytes back into the structure as a result. So "incrementing" the 24-bit pointer becomes: - move 3 bytes to scratch integer "working variable" - increment the "working variable" integer - repack the 3 bytes in 24-bit structure Larger offset & decrementing would be handled similarly through integer arithmetic, as might be detecting/throwing over- and underflow exceptions. Naturally there's a fair amount of data-movement overhead implied by this scheme, but it should be quite portable. It's not essential that the "working variable" be merely 32 bits, of course, and on a 64-bit architecture the most efficient datatype would probably be the natural word size. I say portable because you can see that the storage of three bytes can in principle be managed whether the architecture is big endian or little endian. The trickier aspect is presumably the compiler options to allow structures _not_ to be aligned on word boundaries, and this might actually contribute (depending on how many "odd sized" structures need to be read or moved) to a larger overhead than the packing/unpacking aspect, since the alignment options potentially affect the efficiency of more of what the program has to do. regards, mathtalk-ga |
Subject:
Re: 24-bit unsigned int in
From: mathtalk-ga on 18 Mar 2005 13:39 PST |
If you need a lot of these (and I imagine you do, or it wouldn't be an issue), then consider packing these in pairs. Two odd-byte structures can easily be made to coexist on a word boundary. regards, mathtalk-ga |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |