Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How Do I...

Name: Anonymous 2008-08-29 13:31

...access individual bits in a byte in C++?

Name: Anonymous 2008-08-30 8:35

>>30
long
16 bits
You are hereby disqualified from this discussion.

>>24
For an int18 you would normally just use a long (are those extra bits really gonna hurt you?), but if you insist on having the smallest sufficient type you can use the preprocessor to pick the right one.

#include <limits.h>
#include <stdio.h>

typedef
#if SCHAR_MAX >= 131071
    signed char
#elif SHORT_MAX >= 131071
    short
#elif INT_MAX >= 131071
    int
#else
    long // We know it's big enough
#endif
int18;

int main(void)
{
    printf("%d\n", CHAR_BIT * sizeof(int18));
}


It's a bit big, but it's not something you should do all the time anyway (and you could use some preprocessor tricks otherwise).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List