- #1
MexterO
- 12
- 0
Homework Statement
I am trying to create a program that will convert big endian to little endian notation and vice versa. I found a way to represent unsigned integers and display their binary value. Can I use this program somehow to accomplish my goals. I am going the right way?
Homework Equations
#include <stdio.h>
#include <stdlib.h>
void displayBits (unsigned value);
int main(void)
{
unsigned x; // will hold number to turn to binary
printf("Enter an unsigned integer: ");
scanf ("%u",&x); // memory address of x state as &x
displayBits(x);
}
void displayBits(unsigned value)
{
unsigned c;
unsigned displayMask = 1<<31;
printf("%u = ",value);
for (c=1;c<=32;c++){
putchar(value & displayMask ? '1':'0'); //
value <<=1;
}
putchar ('\n');
}
The Attempt at a Solution
I found a program in my c book on how to do bitwise operators in c. I found the following program. I don't get the for loop totally.
Things I do get:
I get the put char statement and that it will AND the binary value of variable value and displayMask.
I also notice that it will print 1 if condition (value & displayMask) evaluates to true and print 0 if its false. Since there is no new line on the prints statement it is effectively "writing" the binary value of value after the equal sign.
Things I don't get:
I don't however get what value <<=1
and unsigned displayMask = 1<<32 does? I don't see how you can assign a shift operation to a unsigned integer variable?