Length of an unsigned integer
i have a 32 bit unsigned integer (represented as U32)
i need to find its size think it is 10 digit number ( 2^32) so how do i allocate its length ? |
HI there,
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; Code:
U32* var = (U32*)malloc(sizeof(U32)); Code:
U32* var = (U32*)malloc(4); By the way, your range will be 0 to (2^(32) -1). |
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:
|
Code:
#include <stdio.h> |
ya think the answer was straightforward but i wonder y i dint notice it all this time time [:)] thanks man , got it !!
|
If you like math:
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.
|
Quote:
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 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. |
All times are GMT -5. The time now is 03:05 AM. |