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

Click For Summary
SUMMARY

This discussion focuses on converting between big endian and little endian formats using C programming. The provided code demonstrates how to display the binary representation of an unsigned integer using bitwise operations. Key functions include displayBits, which utilizes a left shift operator (<=) to manipulate the binary value, and a display mask initialized with 1<<31. The discussion clarifies the mechanics of bitwise operations and their implications in C programming.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of unsigned integers
  • Knowledge of bitwise operators in C
  • Familiarity with endianess concepts
NEXT STEPS
  • Implement a function to convert big endian to little endian in C
  • Explore the use of the memcpy function for byte manipulation
  • Learn about the htonl and ntohl functions for network byte order conversion
  • Study the implications of endianess in data serialization formats
USEFUL FOR

Software developers, particularly those working with low-level programming, systems programming, and network protocols, will benefit from this discussion. It is also valuable for students learning about data representation and manipulation in C.

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
5K
Replies
29
Views
5K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
9
Views
2K