Path: utzoo!attcan!uunet!ingr!jones From: jones@ingr.UUCP (Mark Jones) Newsgroups: comp.lang.c Subject: Re: Unions have actual uses Keywords: What can they be used for? Message-ID: <2717@ingr.UUCP> Date: 24 Oct 88 13:35:46 GMT References: <322@hrc.UUCP> <2699@hound.UUCP> Organization: Intergraph Corp. Huntsville, Al Lines: 26 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.