8 bit Integer?

This is a discussion on 8 bit Integer? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to get an 8 bit integer in C++? If not, why? I can think of some ways ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up 8 bit Integer?

    Is it possible to get an 8 bit integer in C++? If not, why? I can think of some ways to do it like:

    Code:
    struct Int8
    {
    	short int : 8;
    };
    but it would be easier if there was a standard way of doing it.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    char is an 8-bit integer.

    Code:
    char blah;

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Waterbottle View Post
    char is an 8-bit integer.

    Code:
    char blah;
    Most often, yes. But not always.

    My best suggestion is just to have an extra file, something like "ints.h" with typedefs for all int8, uint8, int16, etc, in case you actually need an integer of that size.
    Mostly, an int8 would be a typedef of a signed char.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    unsigned char would be 8 bits.
    normal char would be 7 bits and a sign bit at the very end.

    Just in case that might be an issue for you.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    As was suggested to me in another thread, you can use std::bitset<>. I don't know if it offers direct conversion to base 10 but in any case you can easily transform base 2 to base 10.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    On most machines, char is an integral type of 8 bits. However, the C++ standard doesn't enforce this property, and in some platforms it may not be so (Some mainframes, the PDP-10, etc). If you need such extreme portability, you'd need to include limits.h and test the CHAR_BIT macro, which indicates the amounts of bits per char. If it's different than 8, then you can use a fallback solution like what you have, otherwise, stick to char / unsigned char.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The standard specifies nothing about the size of the variables, just the relative size of the variables (like long > short)
    So you need to do this by yourself, or the OS provides you with some library of variables.

    Why, I don't know. I can imagine that code will be more portable like this. For example an 64bit system can access easier an int64 (i believe), but a 32bit system would have to use two int32 (like a struct) to accomplish this. So you might have performance issues when you port one code to another system. But, if you declare the variables int then there is no such issue.
    What if you design a system that uses 6bit for integers? Then again a int64 would have been really bad for it.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,662
    The standard says that char is 1 byte, however. But a byte does not necessarily have to be 8 bits.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Ah, thank you all very much!!

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by yaya View Post
    Is it possible to get an 8 bit integer in C++? If not, why? I can think of some ways to do it like:

    Code:
    struct Int8
    {
    	short int : 8;
    };
    but it would be easier if there was a standard way of doing it.

    Thanks.
    Note that the sizeof(Int8) will be sizeof(short int), but the behaviour would be like an 8 bit SIGNED integer - so 7 bits + sign bit.

    Also, to expand on the extreme portability situation: If the system has, for example, 9-bit chars, there it will be pretty darn difficult to produce something that actually stored 8-bit integers in for example an array - the only way to properly achieve that would require a whole lot of bit-shifting - it could be done in C++ by making your own operators - but it's probably "a lot of screaming for a little wool" as the blind man said when he sheared the pig.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    There is this nifty little header called stdint.h that defines exact-width integer types. YOu would use e.g. int8_t for an 8 bit integer type.

    NOTE: this is a C99 header file so your DE may either support it or not.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by MWAAAHAAA View Post
    There is this nifty little header called stdint.h that defines exact-width integer types. YOu would use e.g. int8_t for an 8 bit integer type.

    NOTE: this is a C99 header file so your DE may either support it or not.
    You can always use Paul Hsieh's version.

    http://www.azillionmonkeys.com/qed/pstdint.h
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by EVOEx View Post
    Most often, yes. But not always.

    My best suggestion is just to have an extra file, something like "ints.h" with typedefs for all int8, uint8, int16, etc, in case you actually need an integer of that size.
    Mostly, an int8 would be a typedef of a signed char.
    theoretically youa re right, but in fact I know of no mordern system or implementation where char is NOT 8 bits. True portability is great, right up to the point where it starts forcing you to make assumptions like that char will ever be anything but 8 bits.

    and char is 8 bits regardless fo whether it is signed or unsigned.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by abachler
    theoretically youa re right, but in fact I know of no mordern system or implementation where char is NOT 8 bits. True portability is great, right up to the point where it starts forcing you to make assumptions like that char will ever be anything but 8 bits.
    A "couldn't care less if you don't fit my assumptions" variant of Ronix's suggestion would be to do a static assertion that char is exactly 8 bits, and simply do not bother with a fallback solution if it is not.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Replies: 7
    Last Post: 12-10-2004, 07:18 AM
  5. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM

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