You are on page 1of 2

ANSI codes and colours

(non-portable)

First of all, make sure you're checking terminal types or at least providing a monochrome option so people who use
terms that don't support this don't suffer excape characters all over their text.

The codes themselves have a starting sequence, (Escape-[ or \033[) then a set of numbers seperated by semicolons
which may contain a text attribute normal, bold, underlined, blinking...) and foreground and background colours. The
code is closed with an m.

Properties Properties Foreground Background


(disable)
0 normal 30 black 40 black
1 bold 22 unbold 31 red 41 red
4 underline 24 ununderline 32 green 42 green
5 blink 25 unblink 33 yellow 43 yellow
7 inverse 27 uninverse 34 blue 44 blue
9 strike 29 unstrike 35 magenta 45 magent
36 cyan 46 cyan
37 white 47 white
39 default 49 default

Attributes are not as widely supported as simple colours. Often in an X environment the user has chosen a bold font
and bold will have no effect; underlined, blinking and inverse text sometimes fail for no particular reason. The codes
for cancelling an attribute (unbold etc) sometimes do something completely different. Strikethrough is "standard" but
I've never seen it actually work.

For these reasons you're probably better off just sticking with the colours. A C example follows:

#define ANSI_NORMAL "\033[0m"

/* these are all normal text with black background (40) */


#define ANSI_RED "\033[0;31;40m"
#define ANSI_GREEN "\033[0;32;40m"
#define ANSI_YELLOW "\033[0;33;40m"
#define ANSI_BLUE "\033[0;34;40m"
#define ANSI_MAGENTA "\033[0;35;40m"
#define ANSI_CYAN "\033[0;36;40m"
#define ANSI_WHITE "\033[0;37;40m"

#define ANSI_BOLD_RED "\033[1;31;40m"


#define ANSI_UNDERLINE_RED "\033[4;31;40m"
#define ANSI_BLINK_RED "\033[5;31;40m"

...

printf("%sI am blue!%s\n", ANSI_BLUE, ANSI_NORMAL);


Always, always close the text with the \033[0m code (normal text) or else the text after your program ends will be
affected...
Bit Masks

(advanced, non-portable)

Bit masks are where you specify the number of bits to use in a integral member of a class or struct. C has more
restrictions than C++ on this. In particular, only "int", "signed int" or "unsigned int" can have bit masks in C. Many C
compilers ignore bit masks altogether.

First rule of bit masks: Don't. Don't unless you have a really bad need to save a tiny amount of memory, and slowing
down your code a lot isn't a problem (the compiler will have to work around things as they are not properly aligned).
Bit masks are also inherently non-portable and differ on different compilers on different machines. Some compilers
will ignore them totally.
If you really want to go ahead with it, all you do is put a colon after the member followed by the number of bits it
should occupy. Here's an example:

struct TwelveHourTime
{
unsigned hour : 4; //4 bits (16), enough for 1-12
unsigned minute : 6; //6 bits (64), enough for 0-59
unsigned am : 1; //1 bit, on for am off for pm
};

// 0 1 2 3 4 5 6 7 8 9 A
// |_ _ _ _|_ _ _ _ _ _|_|
// hour minute am
You can use bit masks with a blank identifier to add some padding:

struct TwelveHourTime
{
unsigned hour : 4; //4 bits (16), enough for 1-12
unsigned minute : 6; //6 bits (64), enough for 0-59
unsigned : 5; //5 unused bits
unsigned am : 1; //1 bit, on for am off for pm
};

// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// |_ _ _ _|_ _ _ _ _ _|_ _ _ _ _|_|
// hour minute (unused) am
You can a blank identifier with bit mask of 0 to force the next element to align to the boundary of the given type:

struct TwelveHourTime
{
unsigned hour : 4; //4 bits (16), enough for 1-12
char : 0; //push minute to the next byte boundary
unsigned minute : 6; //6 bits (64), enough for 0-59
unsigned am : 1; //1 bit, on for am off for pm
};

// 0 1 2 3 4 5 6 7 8 9 A B C D E
// |_ _ _ _|_ _ _ _|_ _ _ _ _ _|_|
// hour (unused) minute am
Again, bit masks are very non-portable and the exact layout depends on the compiler and platform. The diagrams are a
guide only.

You might also like