Is it possible to get an 8 bit integer in C++? If not, why? I can think of some ways to do it like:
but it would be easier if there was a standard way of doing it.Code:struct Int8 { short int : 8; };
Thanks.
Is it possible to get an 8 bit integer in C++? If not, why? I can think of some ways to do it like:
but it would be easier if there was a standard way of doing it.Code:struct Int8 { short int : 8; };
Thanks.
char is an 8-bit integer.
Code:char blah;
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. ★
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.
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.
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.
Ah, thank you all very much!!
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.
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); //}
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.
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.Originally Posted by abachler
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)