LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Length of an unsigned integer (https://www.linuxquestions.org/questions/programming-9/length-of-an-unsigned-integer-452315/)

Deepak Inbasekaran 06-07-2006 01:47 AM

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 ?

elyk1212 06-07-2006 01:56 AM

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).

elyk1212 06-07-2006 02:02 AM

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.

Deepak Inbasekaran 06-07-2006 02:02 AM

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

Wim Sturkenboom 06-07-2006 03:50 AM

Code:

#include <stdio.h>
#include <string.h>

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.

Deepak Inbasekaran 06-07-2006 04:10 AM

ya think the answer was straightforward but i wonder y i dint notice it all this time time [:)] thanks man , got it !!

elyk1212 06-07-2006 11:54 AM

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).

tuxdev 06-07-2006 05:09 PM

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.

elyk1212 06-07-2006 11:47 PM

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.


All times are GMT -5. The time now is 03:05 AM.