size of long

Discussion in 'C++' started by pembed2003, Apr 23, 2004.

  1. pembed2003

    pembed2003 Guest

    Hi all,
    As an exercise, I am trying to figure out the size of a long in my
    machine without using the sizeof operator. I came up with the
    following:

    int size_of_long(){
    long i = 1,c = 1;
    while(i > 0){
    i<<=1;
    c++;
    }
    return c / 8;
    }

    This works fine but I am afraid that if the code is being run in a
    machine where it doesn't use 2's compliment. It will not work so I
    came up with another function:

    int size_of_long2(){
    long i[2];
    return (long)(i+1) - (long)i;
    }

    This works regardless of what machine the code is running. I wonder if
    there is any other ways to determine the size of long?

    Thanks!
     
    pembed2003, Apr 23, 2004
    #1
    1. Advertising

  2. "pembed2003" <> wrote...
    > Hi all,
    > As an exercise, I am trying to figure out the size of a long in my
    > machine without using the sizeof operator. I came up with the
    > following:
    >
    > int size_of_long(){
    > long i = 1,c = 1;
    > while(i > 0){
    > i<<=1;
    > c++;
    > }
    > return c / 8;


    Why over 8? Shouldn't it be char_bits() or some such, implemented
    the same way?

    > }
    >
    > This works fine but I am afraid that if the code is being run in a
    > machine where it doesn't use 2's compliment. It will not work so I
    > came up with another function:
    >
    > int size_of_long2(){
    > long i[2];
    > return (long)(i+1) - (long)i;
    > }
    >
    > This works regardless of what machine the code is running. I wonder if
    > there is any other ways to determine the size of long?


    Determine the size of 'unsigned long' and report it. IIRC, unsigned
    versions of the arithmetic types have the same size as their signed
    counterparts. For the 'unsigned long' the operation is well-defined
    and they simply turn 0 after you shift them one time too much.

    Victor
     
    Victor Bazarov, Apr 24, 2004
    #2
    1. Advertising

  3. pembed2003

    Siemel Naran Guest

    "pembed2003" <> wrote in message

    > As an exercise, I am trying to figure out the size of a long in my
    > machine without using the sizeof operator. I came up with the
    > following:


    Without pre-processor: sizeof(long)
     
    Siemel Naran, Apr 24, 2004
    #3
  4. pembed2003 wrote:
    > Hi all,
    > As an exercise, I am trying to figure out the size of a long in my
    > machine without using the sizeof operator. [...]


    Try

    #include <iostream>
    #include <limits>

    int main() {
    std::cout << std::numeric_limits<unsigned long int>::digits
    << std::endl;
    }

    This gives you the number of bits in "unsigned long int",
    which is 32 on my system, for example. I'm not sure though
    it's exactly synonymous with sizeof(unsigned long int).

    HTH,
    - J.
     
    Jacek Dziedzic, Apr 24, 2004
    #4
  5. "Jacek Dziedzic" <> wrote...
    > pembed2003 wrote:
    > > Hi all,
    > > As an exercise, I am trying to figure out the size of a long in my
    > > machine without using the sizeof operator. [...]

    >
    > Try
    >
    > #include <iostream>
    > #include <limits>
    >
    > int main() {
    > std::cout << std::numeric_limits<unsigned long int>::digits
    > << std::endl;
    > }
    >
    > This gives you the number of bits in "unsigned long int",
    > which is 32 on my system, for example. I'm not sure though
    > it's exactly synonymous with sizeof(unsigned long int).


    Of course it's not. It's synonymous with sizeof(unsigned long)*CHAR_BIT

    Victor
     
    Victor Bazarov, Apr 24, 2004
    #5
  6. pembed2003

    Bill Seurer Guest

    pembed2003 wrote:

    > int size_of_long2(){
    > long i[2];
    > return (long)(i+1) - (long)i;
    > }
    >
    > This works regardless of what machine the code is running.


    Hmmm. Is it guaranteed that the compiler will not pad between elements
    of an array of simple types?
     
    Bill Seurer, Apr 26, 2004
    #6
  7. pembed2003

    Old Wolf Guest

    Bill Seurer <> wrote:

    > pembed2003 wrote:
    >
    > > int size_of_long2(){
    > > long i[2];
    > > return (long)(i+1) - (long)i;
    > > }
    > >
    > > This works regardless of what machine the code is running.

    >
    > Hmmm. Is it guaranteed that the compiler will not pad between elements
    > of an array of simple types?


    Yes, but it's not guaranteed that it will convert from (long *) to (long).
    Try:
    return (char *)(i+1) - (char *)i;

    Also, long i[1]; would have been sufficient (you're allowed to point
    one-past-the-end of an array as long as you don't dereference).
     
    Old Wolf, Apr 26, 2004
    #7
  8. Bill Seurer wrote:

    > Hmmm. Is it guaranteed that the compiler will not pad between elements
    > of an array of simple types?


    Yes, it is! I'd asked that on this ng once, so now I know :)

    - J.
     
    Jacek Dziedzic, Apr 28, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    682
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,194
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    478
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    805
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,809
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page