size of long

This is a discussion on size of long within the C++ Programming forums, part of the General Programming Boards category; For some reason when I use sizeof(long) and sizeof(long int) it gives me 4, but that is the same size ...

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    size of long

    For some reason when I use sizeof(long) and sizeof(long int) it gives me 4, but that is the same size of an integer on this system. Is that the way it is supposed to be?

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Its working fine :)

    The size of an integer is more to do with the kind of processor (16 bit / 32 bit) you are working with and the OS.

    When you say int i in a 16 bit system, it would give you 2 bytes of memory. But if you create the same int in a 32 bit system, it would give yu 4 bytes.

    To avoid this disparity, you prefer to explictly say short int or long int

    i.e., when you say short int, you are sure that you will atleast get 2 bytes of memory and when you say long int you would atleast get 4 bytes of memory.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Hmm, this is giving me zero
    long int x = pow(2, 32);
    cout << "x long int: " << x << endl;
    this is, of course, assuming I'm getting 8 bits/byte

    I have another question having to do with the sizes of float and double data types, their maximum values and their level of accuracy. It seems that they are both able to give a maximum value of 2 raised to the 16 (2 bytes) and an accuracy of 5 decimal places, is that correct? I seem to be missing something.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Try this

    Code:
    #include <iostream>
    #include <limits>
    using namespace std; // I know this could be done better
    
    int main()
    {
         cout << "largest float val " << numeric_limits<float>::max() << endl;
         cout << "largest double val " << numeric_limits<double>::max() << endl;
         cout << "smallest float val " << numeric_limits<float>::min() << endl;
         // try it for various datatypes.. you will get to know
         // the limits on your system
         return 0;
    }
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Silvercord
    Hmm, this is giving me zero
    long int x = pow(2, 32);
    cout << "x long int: " << x << endl;
    this is, of course, assuming I'm getting 8 bits/byte
    you're getting 0 because pow returns an int, NOT an unsigned int. The capacity of int in the positive direction is roughly half that of an unsigned int. get it?

    edit: on the other hand, wouldn't 2 raised to the 32 set the 33rd bit?
    Last edited by FillYourBrain; 01-28-2003 at 07:17 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    The max value that a 32bit int can hold is 2^32 - 1. Doing pow(2,32) overflows.

  7. #7
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    edit: on the other hand, wouldn't 2 raised to the 32 set the 33rd bit?
    why would that be true? I would need to know if that's true or not.

    EDIT: You are right fill you brain, I just tried doing it myself, but I'm still not exactly sure why that is so. does it have to do with the fact that any number raised to the 0 power is 1? Does pow() automatically skip that or something?
    Last edited by Silvercord; 01-28-2003 at 12:24 PM.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    try pow(10,4), how many digits do you need? What if you had only 4? Sign makes this even more complicated but for every negitive number you can represent in the same space you loose that pattern to represent a positive number.

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Silvercord
    why would that be true? I would need to know if that's true or not.

    EDIT: You are right fill you brain, I just tried doing it myself, but I'm still not exactly sure why that is so. does it have to do with the fact that any number raised to the 0 power is 1? Does pow() automatically skip that or something?
    think about it...


    2 raised to the 1 is two right?
    000010 <-second bit

    2 raised to the 2 is 4
    000100 <-3rd bit
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    that makes sense and I'm glad that you pointed that out, but the reason I was so freakin confused was that the first power IS 0! (and let's not make any jokes about how my database programs always access memory that doesn't exist, sorry part is i do that sometimes )

    EDIT:

    What is the difference between long and int? they both have the same limit!

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    shiv_tech_quest answered that one. on 32 bit systems, int is the same as long. on 16 bit systems, int was the same as a short.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Oh ok, I guess I didn't understand that's what he meant

    EDIT: On 32 bit systems you can have a data type that holds twice the number of bits that the processor can hold at one time (double), was the same true on 16 bit systems?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  3. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  4. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  5. Background-process app
    By philip in forum C++ Programming
    Replies: 14
    Last Post: 03-26-2002, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21