Newsgroups: algonquinc.academic.courses.dat.2343.01f.announce From: idallen@freenet.carleton.ca (Ian! D. Allen) Organization: The National Capital FreeNet, Ottawa, Ontario, CANADA Subject: Right and Left shift Date: Wed, 5 Sep 2001 18:43:35 +0000 (UTC) I see evidence that I mis-labelled right and left shift in some classes this week. Here's the truth with a few examples each: Left Shift (LSH) 1 bit: x = x << 1; Before: 00001000 ( = 8 decimal) After: 00010000 ( = 16 decimal ) Before: 10010010 After: 00100100 ( one bit is "lost" off the top) Right Shift (RSH) 1 bit: x = x >> 1; Before: 00001000 ( = 8 decimal ) After: 00000100 ( = 4 decimal ) Before: 01001001 After: 00100100 ( one bit is "lost" off the bottom) A real C program using shifts and masking: /* C Program to decode the Unix return status of a command. * If the command completes normally, the exit status is zero. * If you interrupt it, you see a non-zero signal status. * Compile and run this on a Unix system, e.g. ACADAIX: * * $ cc thisfile.c * $ ./a.out * 'sleep 5' terminated with exit status 0 and signal 0 * $ ./a.out * ^C (Interrupt) * 'sleep 5' terminated with exit status 0 and signal 2 * * -IAN! idallen@ncf.ca */ #include #define COMMAND "sleep 5" int main( void ){ int status = system(COMMAND); int exitstatus = status >> 8; int signalstatus = status & 0xFF; printf("'%s' terminated with exit status %d and signal %d\n", COMMAND, exitstatus, signalstatus); return status; } -- -IAN! Ian! D. Allen Ottawa, Ontario, Canada idallen@ncf.ca Home Page on the Ottawa FreeNet: http://www.ncf.ca/~aa610/ College professor at: http://www.algonquincollege.com/~alleni/ Board Member, TeleCommunities CANADA http://www.tc.ca/