[comp.lang.c] Unions have actual uses

jones@ingr.UUCP (Mark Jones) (10/24/88)

An Internet address is a 32 bit quantity, that can have 3 different meanings.

A Class A network has the MSB set to 0, the next seven bits are the
network number, and the last 24 are the host number.

A Class B network has the MSB set to 1, the next bit set to 1, the next
14 bits are the network nymber, and the last 16 are the host number.

A Class C network has the MSB set to 1, then next bit set to 1, and the
3rd MSB set to 0, the next 21 bits are the network number, and the last
8 are the host number.

Union InterNetAddress
{
	unsigned long addr_l;
	unsigned short addr_s[2];
	unsigned char addr_c[4];
};

gives you a chance to pull the address apart in a more readable manner. 
Bit fields can also be used in the union, and we do, but since bitfield
arrangements are somewhat compiler specific, I didn't show them.

This is where unions come in very handy.