If you put your loop into a separate function you can then call it twice, once with parameter 'x' and once with parameter x >> 4.
Paul
Main Topics
Browse All Topics#include <stdio.h>
#include <stdlib.h>
int main( void )
{
unsigned x; /* variable to hold user input */
unsigned c; /* counter */
printf( "Enter an unsigned integer: " );
scanf( "%u", &x );
/* define displayMask and left shift 31 bits */
unsigned displayMask = 1 << 31;
printf( "%10u = ", x );
/* loop through bits */
for ( c = 1; c <= 32; c++ ) {
putchar( x & displayMask ? '1' : '0' );
x <<= 1; /* shift value left by 1 */
if ( c % 8 == 0 ) { /* output space after 8 bits */
putchar( ' ' );
} /* end if */
} /* end for */
putchar( '\n' );
system("pause");
return 0;
} /* end function displayBits */
I have got this far but now that I have printed the bits without moving it over 4 to the right I need to print it again with the bits moved over 4 bits. I have hit a wall.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: jkrPosted on 2007-09-18 at 09:49:10ID: 19914558
You are looking for the "Bitwise Left Shift and Right Shift Operators: <<, >>". The bitwise shift operators shift their first operand left (<<) or right (>>) by the number of positions the second operand specifies. So, for your purpose, that would be
unsigned short us = 16; // set MSB
us = us >> 4; // move four bits