LinuxQuestions.org Length of an unsigned integer
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 06-07-2006, 01:47 AM #1 Deepak Inbasekaran Member   Registered: Apr 2006 Location: India Distribution: Red Hat Linux release 9 (Shrike) Posts: 44 Rep: 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 ?
 06-07-2006, 01:56 AM #2 elyk1212 Member   Registered: Jan 2005 Location: Chandler, AZ USA Distribution: Mandrake/Mandriva 10.2 Posts: 186 Rep: 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;` If you are using it as a pointer on the heap, allocate it as such: Code: `U32* var = (U32*)malloc(sizeof(U32));` Or instead of size of just do: Code: `U32* var = (U32*)malloc(4);` ... since you know it is a 32 bit number. By the way, your range will be 0 to (2^(32) -1). Last edited by elyk1212; 06-07-2006 at 02:05 AM.
 06-07-2006, 02:02 AM #3 elyk1212 Member   Registered: Jan 2005 Location: Chandler, AZ USA Distribution: Mandrake/Mandriva 10.2 Posts: 186 Rep: 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. Last edited by elyk1212; 06-07-2006 at 02:04 AM.
06-07-2006, 02:02 AM   #4
Deepak Inbasekaran
Member

Registered: Apr 2006
Location: India
Distribution: Red Hat Linux release 9 (Shrike)
Posts: 44

Original Poster
Rep:
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 message2
so to read from end i need to know the exact length of the U32 number

 06-07-2006, 03:50 AM #5 Wim Sturkenboom Senior Member   Registered: Jan 2005 Location: Roodepoort, South Africa Distribution: Ubuntu 12.04, Antix19.3 Posts: 3,797 Rep: Code: ```#include #include int main() { unsigned int myvalue; char buffer[255]; myvalue=(unsigned int) -1; printf("hex value = 0x%X\n",myvalue); printf("dec value = %u\n",myvalue); sprintf(buffer,"%u",myvalue); printf("buf value = %s\n",buffer); printf("strlen = %d digits\n", strlen(buffer)); return 0; }``` The last statement before the return will give you the max number of digits in a 32 bit unsigned int.
 06-07-2006, 04:10 AM #6 Deepak Inbasekaran Member   Registered: Apr 2006 Location: India Distribution: Red Hat Linux release 9 (Shrike) Posts: 44 Original Poster Rep: ya think the answer was straightforward but i wonder y i dint notice it all this time time [] thanks man , got it !!
 06-07-2006, 11:54 AM #7 elyk1212 Member   Registered: Jan 2005 Location: Chandler, AZ USA Distribution: Mandrake/Mandriva 10.2 Posts: 186 Rep: 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).
 06-07-2006, 05:09 PM #8 tuxdev Senior Member   Registered: Jul 2005 Distribution: Slackware Posts: 2,012 Rep: 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.
06-07-2006, 11:47 PM   #9
elyk1212
Member

Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep:
Quote:
 ..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.

Last edited by elyk1212; 06-07-2006 at 11:49 PM.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post ArthurHuang Programming 4 05-23-2006 03:46 AM alaios Programming 4 06-03-2005 09:34 AM linux_lover2005 Programming 3 04-26-2005 11:38 PM george_mercury Programming 2 11-27-2004 01:27 PM daztheladd Programming 5 11-26-2002 07:29 AM

LinuxQuestions.org

All times are GMT -5. The time now is 02:06 AM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -