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

In summary: In other words, the variable displayMask gets the binary value 0000 ... 0000 0000, because the 1 has been shifted off the end.
  • #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?
 
Physics news on Phys.org
  • #2
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.
 

1. What is the difference between Big Endian and Little Endian?

Big Endian and Little Endian refer to two different ways of storing binary data in a computer's memory. In Big Endian, the most significant byte (the leftmost byte) is stored first, while in Little Endian, the least significant byte (the rightmost byte) is stored first.

2. Why do we need to know about Big Endian and Little Endian?

Understanding Big Endian and Little Endian is important when working with binary data, as it determines how data is stored and interpreted by the computer. If data is not stored in the correct endian format, it can lead to errors in data retrieval and processing.

3. How do we determine the endianess of a system?

The endianess of a system can be determined by examining the first byte of a multi-byte data type, such as an integer. If the first byte is the most significant byte, the system is Big Endian. If the first byte is the least significant byte, the system is Little Endian.

4. How do we code for reversing a binary value in Big Endian and Little Endian?

In Big Endian, reversing a binary value simply involves swapping the position of the bytes, i.e. moving the most significant byte to the end. In Little Endian, the bytes do not need to be swapped, but the bits within each byte need to be reversed. This can be achieved using bitwise operations in most programming languages.

5. What are the advantages and disadvantages of using Big Endian and Little Endian?

Big Endian is more intuitive for humans to read and understand, as the most significant digits are displayed first. However, Little Endian is more efficient for processing data, as it allows for faster retrieval of data. The choice of which endian format to use often depends on the specific application and compatibility with existing systems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
29
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
669
  • Engineering and Comp Sci Homework Help
Replies
3
Views
883
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
Back
Top