[Dev-C++] reversing bit to be displayed
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Mitchell, T. (HNA) <Tam...@hj...> - 2001-02-05 16:21:36
|
I am attempting to write a program that reverses the order of the bits in an
unsigned integer value. The program should input the value from the user
and call function reverseBits to print the bits in reverse order. Print the
value both before and after the bits are reversed.
Any suggestions?
#include <stdio.h>
#include <stdlib.h>
void reverseBits(unsigned);
void displayBits(unsigned);
int main()
{
unsigned number;
// number = 21845;
printf("enter a numnber \n");
scanf("%u", &number);
printf("\n The reverse of \n");
displayBits(number);
printf("is\n");
reverseBits(number);
// reverseBits(~number);
system("PAUSE");
return 0;
}
void displayBits(unsigned value)
{
unsigned c, displayMask = 1 << 15;
printf("%7u = ", value);
for (c=1; c <=16; c++)
{
putchar(value & displayMask ? '1' : '0');
value <<= 1;
if(c % 8 == 0)
putchar(' ');
}
putchar('\n');
}
void reverseBits(unsigned value)
{
unsigned c, displayMask = 1 << 15;
unsigned newval = 0;
for (c=1; c<=16; c++)
{
newval += (value & displayMask ? '1' : '0');
value <<= 1;
newval >>= 1;
if (c%8 == 0)
putchar(' ');
}
putchar('\n');
displayBits(newval);
}
Tamra Y. Mitchell
Heinz North America
Operating Systems Analyst
(412) 237-5232 Work
(412) 996-9078 Cell Phone
|