#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 2_6.c 2_6.cmp 2_6a.c 2_6b.c 2_6c.c makefile # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 22:57:08 EDT 1990 # echo x - 2_6.c sed 's/^X//' > 2_6.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // print the bits of a null character pointer #include #include #include "2_6a.c" /* EXPAND */ #include "2_6c.c" /* EXPAND */ void prbits(char byte) { cout << "0b"; for (unsigned long mask = HIBIT(unsigned char); mask != 0; mask >>= 1) if ((byte & mask) != 0) cout << "1"; else cout << "0"; } int main(int, char**) { #include "2_6b.c" /* EXPAND4 */ for (int i = 0; i < sizeof(char*); i++) { cout << "byte " << i << " = "; prbits(pi.byte[i]); cout << "\n"; } return 0; } !EOF! ls -l 2_6.c echo x - 2_6.cmp sed 's/^X//' > 2_6.cmp << '!EOF!' byte 0 = 0b00000000 byte 1 = 0b00000000 byte 2 = 0b00000000 byte 3 = 0b00000000 !EOF! ls -l 2_6.cmp echo x - 2_6a.c sed 's/^X//' > 2_6a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ union ptr_bits { char *ptr; char byte[sizeof(char*)]; }; !EOF! ls -l 2_6a.c echo x - 2_6b.c sed 's/^X//' > 2_6b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ ptr_bits pi; pi.ptr = 0; !EOF! ls -l 2_6b.c echo x - 2_6c.c sed 's/^X//' > 2_6c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #undef BITS /* DELETE */ #define BITS(type) (CHAR_BIT * (int)sizeof(type)) #define HIBIT(type) ((type)(((type) 1) << \ (BITS(type) - 1))) !EOF! ls -l 2_6c.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC all: 2_6 2_6: 2_6.c 2_6a.c 2_6b.c 2_6c.c $(CC) 2_6.c -o 2_6 test: all 2_6.cmp 2_6 > 2_6.out cmp 2_6.out 2_6.cmp echo tests done !EOF! ls -l makefile # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0