ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am sorry, I do not know what you mean, exactly. The size is 32 bits, so you need to allocate 4 bytes. If you are using this as a local variable, just declare it as:
Code:
U32 var;
If you are using it as a pointer on the heap, allocate it as such:
Or... do you ... want to find out how many digits there are in the unsigned 32 bit number, is this correct?
If you want to know how many 10s places there are just look and do a % 10, keep the current number and count till you reach 0. You can just use another U32 to hold the count, it is ok to use all 32 bits.
Why is this the case?
It is less costly to use this larger data type when you are using a 32 bit system since it will require less operations. E.g. if you saved it in a byte you would need to AND by 0xFF in each operations, which is pointless (Google optimizing C code). It is likely that you are on a 32 bit system, unless you are doing something with embedded systems.
actually in my code , i have a U32 index attached to every message that i store in a file, and i got to get the U32 index number that i have attached to the last msg in that file, so i got to read for that particular length of U32 to get that number appended to my last msg.
Quote:
eg:
message1<U32>
message2<U32>
so to read from end i need to know the exact length of the U32 number
Also, you can know by just by base 2 math, like I was mentioning... Max is: (2^(32) -1) = 4,294,967,295. It is always this for unsigned 32 bit integers. Example... Think about it for 8 bits, unsigned range is 0 to 255 = 2^8 -1., for 3 bits, 0 to 7 = 2^3 -1. Floating Point is much more involved, integers are straight forward.
If it were just an unsigned integer and you were not sure of the bit length (may vary per OLD compiler or architecture.. but almost always 4 bytes), you would want to do something like Wim mentioned, as to have a general way to find the length. However, the type dictates the length in this case (U32).
An int is the natural processing type for a machine. Therefore, for older computers, an int was 16 bits. DOS games tend to be hard to port because all there ints were 16 bits and the level data was binary, and possibly in a different endianess than the target arch. Newer computers may have 64 bit integers. Oh, and remember to adjust for endianess. Or just go with a textual representation, which means that it shouldn't really matter how big an integer is if properly implemented.
..want to know how many 10s places there are just look and do a % 10, keep the current number and count till you reach 0. You can just use another U32 to hold the count, it is ok to use all 32 bits.
I meant to say divide, not modulo.
By the way, it is more efficient to do this than to produce a char array on the stack, convert a number to ascii characters, and then iterate through the string to find the length. That is a lot of extra stuff to only find the length of an integer in decimal digits.
If you are doing code where speed matters (embedded systems), you would want to do something like:
Example:
Code:
// Put the largest possible unsigned
// 32bit number into num
U32 num = 0xFFFFFFFF;
int i = 0;
while(num != 0)
{
i++;
num = num / 10;
}
printf("The number of decimal digits is: %d",i);
It is less instructions and your boss/professor/client will probably like 'fast'. Further optimizations may be possible, but most I know of are architecture/compiler specific (or I just don't know them, yet).
Anyway, Im just trying to improve on the good ideas expressed.
To tuxdev:
Yes, it is cool, now they have 64bit bus, and registers which lend itself nicely to 64bit data types. I have not had the privilege to develop on one of these machines yet though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.