From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/PROBLEMS/binary_op.c | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 reference/C/PROBLEMS/binary_op.c (limited to 'reference/C/PROBLEMS/binary_op.c') diff --git a/reference/C/PROBLEMS/binary_op.c b/reference/C/PROBLEMS/binary_op.c new file mode 100644 index 0000000..2e5a214 --- /dev/null +++ b/reference/C/PROBLEMS/binary_op.c @@ -0,0 +1,53 @@ +/************************************************************************ + * + * Purpose: Display a byte in binary format. + * + * Author: M.J. Leslie. + * + * Date: 19-Mar-95 + * + * Improvements: + * 1) Return the result to the calling function. + * 2) Make it work on integers (4 bytes long). + * + ************************************************************************/ + +void binary_op(char byte); /* Print a byte in binary. */ + +/************************************************************************/ + +main() + { + char byte=55; + + binary_op(byte); /* Print a byte in binary. */ + } + +/************************************************************************/ + +void binary_op(char byte) + { + int count=8; /* Number of bits in a byte. */ + + while(count--) + { + /* AND the high order bit (the + * left one) If the bit is set, + * print a ONE. */ + printf("%d", ( byte & 128 ) ? 1 : 0 ); + + /* Move all the bits LEFT. */ + byte <<= 1; + } + + printf("\n"); + } + +/************************************************************************ + * + * Program results. + * ---------------- + * + * 00110111 + * + ************************************************************************/ -- cgit v1.2.3-54-g00ecf