Data Type Size Question

Discussion in 'C++' started by Immortal Nephi, Jun 9, 2009.

  1. C++ Compiler provides four data type sizes (char, short, long, and
    long long). Sometimes, programmers prefer 32 bit size over 16 bit
    size or 8 bit size. They say 16 bit size is slower than 8 bit size
    and 32 bit size on x86 machine.

    What choice do programmers prefer? For example.

    unsigned char byte = 0x03;
    byte++;

    unsigned short byte = 0x03;
    ....or...
    unsigned long byte = 0x03;

    byte++;
    byte &= 0xFF;

    If you choose unsigned char, then you don't need to add "byte &=
    0xFF".

    unsigned short word = 0x2003;
    word++;

    unsigned long word = 0x2003;

    word++;
    word &= 0xFFFF;

    If you choose unsigned short, then you don't need to add "word &=
    0xFFFF".

    Sometimes, C++ Compiler's optimization loads data from either 8 bit
    variable or 16 bit variable into 32 bit register. The x86
    instructions work to do calculation on 32 bit register before it is
    stored back to 8 bit variable or 16 bit variable. It might cost more
    clock cycles.

    Sometimes, programmers choose two 8 bit variables instead of one 16
    bit variable. For example

    unsigned char low_byte = 0x03;
    unsigned char high_byte = 0x20;

    unsigned short word = 0x2003;

    Programmers can work to modify high byte directly. They don't need to
    use right 8 bit shift ( high_byte >> 8 ) to modify high byte before it
    is left 8 bit shift back to 16 bit variable.

    If they choose 16 bit variable instead of 8 bit variable, then they
    are required to use both left 8 bit shift and right 8 bit shift
    because they don't use them very frequently.

    Please let me know the best practices they prefer. Thanks...
     
    Immortal Nephi, Jun 9, 2009
    #1
    1. Advertising

  2. Immortal Nephi

    red floyd Guest

    Immortal Nephi wrote:
    > C++ Compiler provides four data type sizes

    correct
    (char, short, long, and
    > long long).


    Incorrect, at least for C++03. Long long is not defined in C++03, but
    it is in C++0x.


    In C++03 the sizes are char, short, int and long.
     
    red floyd, Jun 9, 2009
    #2
    1. Advertising

  3. On Jun 9, 10:42 am, red floyd <> wrote:
    > Immortal Nephi wrote:
    > > C++ Compiler provides four data type sizes

    >
    > correct
    >   (char, short, long, and
    >
    > > long long).

    >
    > Incorrect, at least for C++03.  Long long is not defined in C++03, but
    > it is in C++0x.
    >
    > In C++03 the sizes are char, short, int and long.


    Is Microsoft C++ Compiler (from Visual Studio .Net 2008) C++03?

    I thought that int can be either 16 bit or 32 bit on 32 bit machine
    and either 32 bit or 64 bit on 64 bit machine. Correct?

    Can you please tell me your opinion? Which data type do programmers
    prefer? If they don't want to use "&= 0xFF" or "&= 0xFFFF" then they
    choose either char or short size. If they don't use ">> 8" and "<< 8"
    very frequent, then they choose two char variables instead of one
    short variable or int variable. Thanks...
     
    Immortal Nephi, Jun 9, 2009
    #3
  4. Immortal Nephi

    red floyd Guest

    Immortal Nephi wrote:
    > On Jun 9, 10:42 am, red floyd <> wrote:
    >> Immortal Nephi wrote:
    >>> C++ Compiler provides four data type sizes

    >> correct
    >> (char, short, long, and
    >>
    >>> long long).

    >> Incorrect, at least for C++03. Long long is not defined in C++03, but
    >> it is in C++0x.
    >>
    >> In C++03 the sizes are char, short, int and long.

    >
    > Is Microsoft C++ Compiler (from Visual Studio .Net 2008) C++03?
    >


    Some compilers support long long as an extension.
     
    red floyd, Jun 9, 2009
    #4
  5. Immortal Nephi

    Fred Guest

    On Jun 9, 10:53 am, red floyd <> wrote:
    > Immortal Nephi wrote:
    > > On Jun 9, 10:42 am, red floyd <> wrote:
    > >> Immortal Nephi wrote:
    > >>> C++ Compiler provides four data type sizes
    > >> correct
    > >>   (char, short, long, and

    >
    > >>> long long).
    > >> Incorrect, at least for C++03.  Long long is not defined in C++03, but
    > >> it is in C++0x.

    >
    > >> In C++03 the sizes are char, short, int and long.

    >
    > > Is Microsoft C++ Compiler (from Visual Studio .Net 2008) C++03?

    >
    > Some compilers support long long as an extension.


    What about float and doubble?

    I thought the int-type sizes were only required to follow:

    sizeof(short) <= sizeof(int) <= sizeof(long)

    All three *could* be the same length
    --
    Fred K


    --
    Fred K
     
    Fred, Jun 9, 2009
    #5
  6. Immortal Nephi

    Camiel Guest

    With the char, short, int, long and long long data types there are no
    guarantees as to their size, except that char <= short <= int <= long
    <= long long. Its up to the compiler writer to choose sizes that make
    sense on the platform. For example, on 36-bit machines (PDP-10 for
    instance) it was not uncommon to have a 9-bit char type.

    I write hardware emulators for a living, so I often really _do_ care
    about how many bits my data types are. If that is the case, and if the
    code needs to be portable, I will include a separate header file to
    define my own data types based on what data types the compiler has to
    offer. If available, I will use the C99 int8_t, int16_t... data types,
    or Microsoft's __int8, __int16.... I then use whatever fits. If the
    value I'm working with is an 8-bit value, I'll use the 8-bit data type
    I defined.
     
    Camiel, Jun 10, 2009
    #6
  7. Immortal Nephi wrote:

    > C++ Compiler provides four data type sizes (char, short, long, and
    > long long).


    You are missing int, and long long is not required to be available.

    > Sometimes, programmers prefer 32 bit size over 16 bit
    > size or 8 bit size. They say 16 bit size is slower than 8 bit size
    > and 32 bit size on x86 machine.
    >
    > What choice do programmers prefer?


    Use the type that most clearly expresses your intent.
    The only reason to use one of the tricks you showed is because
    measurements have shown that the code with the original type was causing
    a bottleneck in the program and these changes gave a measurable
    improvement.

    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://c-faq.com/
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
     
    Bart van Ingen Schenau, Jun 10, 2009
    #7
  8. Immortal Nephi

    James Kanze Guest

    On Jun 9, 5:22 pm, Immortal Nephi <> wrote:
    > C++ Compiler provides four data type sizes (char, short, long,
    > and long long).


    The current C++ standard provides for up to four sizes for
    integral data: char, short, int and long. The next version of
    the standard (and the current C standard) provide for more: they
    requrie support for long long, and allow an implementation to
    define additional integral types.

    Of course, there's no requirement that any of these types
    actually have different sizes; it's quite frequent for short,
    int and long to all have the same size, for example, and I've
    heard that there are embedded processors where even char has the
    same size as the other types.

    And, of course, pointers, floating point, etc. may have still
    other sizes.

    > Sometimes, programmers prefer 32 bit size over 16 bit size or
    > 8 bit size. They say 16 bit size is slower than 8 bit size
    > and 32 bit size on x86 machine.


    > What choice do programmers prefer?


    Normally, unless there are specific requirements otherwise, a
    programmer should just use int, and be done with it. That's
    what it's there for.

    > For example.


    > unsigned char byte = 0x03;
    > byte++;


    > unsigned short byte = 0x03;
    > ...or...
    > unsigned long byte = 0x03;


    > byte++;
    > byte &= 0xFF;


    > If you choose unsigned char, then you don't need to add "byte
    > &= 0xFF".


    That depends. If you're not worried about portability to
    machines with bytes sizes other than 8, fine. If you want
    exactly 8 bits, however, you need the & 0xFF in truly portable
    code. And of course, if the byte size is 8, I would expect the
    compiler to generate exactly the same code whether you wrote the
    &= 0xFF or not.

    > unsigned short word = 0x2003;
    > word++;


    > unsigned long word = 0x2003;


    > word++;
    > word &= 0xFFFF;


    > If you choose unsigned short, then you don't need to add "word
    > &= 0xFFFF".


    Provided that short is 16 bits. It often isn't.

    > Sometimes, C++ Compiler's optimization loads data from either
    > 8 bit variable or 16 bit variable into 32 bit register. The
    > x86 instructions work to do calculation on 32 bit register
    > before it is stored back to 8 bit variable or 16 bit variable.
    > It might cost more clock cycles.


    > Sometimes, programmers choose two 8 bit variables instead of
    > one 16 bit variable. For example


    > unsigned char low_byte = 0x03;
    > unsigned char high_byte = 0x20;


    > unsigned short word = 0x2003;


    > Programmers can work to modify high byte directly. They don't
    > need to use right 8 bit shift ( high_byte >> 8 ) to modify
    > high byte before it is left 8 bit shift back to 16 bit
    > variable.


    So? When is that relevant.

    The only time I've seen programmers using multiple bytes instead
    of a larger integral type is when marshalling, in order to not
    have to worry about byte order and alignment.

    > If they choose 16 bit variable instead of 8 bit variable, then
    > they are required to use both left 8 bit shift and right 8 bit
    > shift because they don't use them very frequently.


    > Please let me know the best practices they prefer. Thanks...


    The best practice is to use int for integral values, char for
    characters, unsigned char for raw memory, and only use the other
    integral types when there are specific constraints (int not
    large enough, etc.).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 10, 2009
    #8
  9. Immortal Nephi

    James Kanze Guest

    On Jun 9, 6:37 pm, Immortal Nephi <> wrote:
    > On Jun 9, 10:42 am, red floyd <> wrote:


    > > Immortal Nephi wrote:
    > > > C++ Compiler provides four data type sizes


    > > correct
    > > (char, short, long, and


    > > > long long).


    > > Incorrect, at least for C++03. Long long is not defined in
    > > C++03, but it is in C++0x.


    > > In C++03 the sizes are char, short, int and long.


    > Is Microsoft C++ Compiler (from Visual Studio .Net 2008) C++03?


    > I thought that int can be either 16 bit or 32 bit on 32 bit
    > machine and either 32 bit or 64 bit on 64 bit machine.
    > Correct?


    int can be anything 16 bits or more. I personally know of cases
    where it is 16, 32, 36 and 48; I think other values have also
    occured.

    > Can you please tell me your opinion? Which data type do
    > programmers prefer?


    For integral types, int is the normal type---anything else is
    only used for special cases.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 10, 2009
    #9
  10. Immortal Nephi

    James Kanze Guest

    On Jun 9, 8:20 pm, Fred <> wrote:
    > On Jun 9, 10:53 am, red floyd <> wrote:
    > > Immortal Nephi wrote:
    > > > On Jun 9, 10:42 am, red floyd <> wrote:
    > > >> Immortal Nephi wrote:
    > > >>> C++ Compiler provides four data type sizes
    > > >> correct
    > > >> (char, short, long, and


    > > >>> long long).
    > > >> Incorrect, at least for C++03. Long long is not defined
    > > >> in C++03, but it is in C++0x.


    > > >> In C++03 the sizes are char, short, int and long.


    > > > Is Microsoft C++ Compiler (from Visual Studio .Net 2008) C++03?


    > > Some compilers support long long as an extension.


    > What about float and doubble?


    > I thought the int-type sizes were only required to follow:


    > sizeof(short) <= sizeof(int) <= sizeof(long)


    > All three *could* be the same length


    And are on some architectures. For that matter, all could be
    equal to 1 (and thus be the same size as char)---I think some
    embedded processors use this.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 10, 2009
    #10
    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. luna
    Replies:
    1
    Views:
    6,828
  2. heyo
    Replies:
    3
    Views:
    923
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    802
    Dan Pop
    Apr 2, 2004
  4. Yevgen Muntyan

    #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type)))

    Yevgen Muntyan, Feb 9, 2007, in forum: C Programming
    Replies:
    10
    Views:
    915
    Yevgen Muntyan
    Feb 13, 2007
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,606
    Michael Jung
    May 25, 2008
Loading...

Share This Page