Big endian and Little Endian, how to code for reversing binary value?

Click For Summary
The discussion focuses on creating a program to convert between big endian and little endian formats using C. The original poster successfully displays the binary representation of unsigned integers but seeks clarification on specific code elements, particularly the left shift operator (<<) and its implications. It is explained that the left shift operator shifts bits to the left, effectively multiplying the number by two for each shift, and that using 1<<32 results in a value of zero for a 32-bit unsigned integer. The conversation emphasizes understanding bitwise operations and their role in manipulating binary values for endian conversion. Overall, the thread highlights the importance of grasping these concepts to achieve the desired programming goals.
MexterO
Messages
12
Reaction score
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?
 
Physics news on Phys.org
MexterO said:

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

When your post includes code, put [ code ] and [ /code ] tags around it (omit the extra spaces, though).
MexterO said:
Code:
#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
In C and C++ << is the left shift operator. Similarly, >> is the right shift operator. You can combine shifting and assignment with the <<= operator.

In your example above, all of the bits in value get shifted one place to the left, and this new value is assigned to the value variable. For example, if value == 5, its bit pattern looks like this 0000 ... 0000 0101.
Shifting the bits one place to the right results in 0000 ... 0000 1010 (which is decimal 10), and this is the value that is stored in the variable. You might notice that we ended up with a value twice as big as what we started with, and this is not an accident. A left shift of one place is equivalent to multiplying by 2; a left shift of two places is equivalent to multiplying by 22, and so on.
MexterO said:
and unsigned displayMask = 1<<32 does? I don't see how you can assign a shift operation to a unsigned integer variable?
This starts with 1, and shifts it 32 positions to the left. However, if the unsigned type is 32 bits, this operation will shift the 1 off the end of the displayMask variable, resulting in 32 bits of zeroes.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
29
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
9
Views
2K