C programming Consider this code typedef union int n unsigne

C programming

Consider this code.

typedef union{

int n;

unsigned char bytes [4];

}MyUnion;

MyUnion u = { .bytes = { 0x57, 0x68, 0x6f, 0x00 } };

printf(\"%x\ \", (short)u.n);

1) what does this code print out on a big-endian computer? Briefly explain why.

2) What does this code print out on a little-endian computer? Briefly explain why

Solution

1) what does this code print out on a big-endian computer? Briefly explain why.

higher memory

          ----->

    +----+----+----+----+

    |0x01|0x00|0x00|0x00|

    +----+----+----+----+

    A

    |

   &x

Suppose we are on a 32-bit machine. so (char*)(*x) == 1 , and *y+48 == \'1\' . unsigned int x = 1; printf (\"%d\", (int) (((char *)&x)[0])); And setting &x to char * will enable you to access the individual bytes of the integer, and the ordering of bytes will depend on the endianness of the system.

Big Endian

In big endian, you store the most significant byte in the smallest address. Here\'s how it would look:

Address :

1. 1000

2. 1001

3.1002

4.1003

Value:

1. 90

2. AB

3. 12

4. CD

Once you start thinking about endianness, you begin to think it applies to everything. Before you see big or little endian, you may have had no idea it even existed. That\'s because it\'s reasonably well-hidden from you.

If you do bitwise/bitshift operations on an int, you don\'t notice the endianness. The machine arranges the multiple bytes so the least significant byte is still the least significant byte (e.g., b7-0) and the most significant byte is still the most significant byte (e.g., b31-24).

So, it\'s natural to think whether strings might be saved in some sort of strange order, depending on the machine.

This is where it\'s useful to think about all the facts you know about arrays. A C-style string, after all, is still an array of characters.

Here are some facts you should know about C-style strings and arrays.

2) What does this code print out on a little-endian computer? Briefly explain why

Suppose we are on a 32-bit machine. so (char*)(*x) == 1 , and *y+48 == \'1\' . unsigned int x = 1; printf (\"%d\", (int) (((char *)&x)[0])); And setting &x to char * will enable you to access the individual bytes of the integer, and the ordering of bytes will depend on the endianness of the system

Little Endian

In little endian, you store the least significant byte in the smallest address. Here\'s how it would look:

Address:

1. 1000

2. 1001

3. 1002

4.1003

Value:

1. CD

2. 12

3. AB

Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.

For example, a 4 byte, 32-bit integer


Byte3 Byte2 Byte1 Byte0


will be arranged in memory as follows:


Base_Address+0 Byte0
Base_Address+1 Byte1
Base_Address+2 Byte2
Base_Address+3 Byte3


Intel processors use \"Little Endian\" byte order.

Ex:

#include <stdio.h>
int main () {
int num, i, val;
char *ptr;

/* get the integer input from user */
printf(\"Enter your input(1 - 15):\");
scanf(\"%d\", &num);

if (num < 1 || num > 15) {
printf(\"Thresh Level Exceeded!!\ \");
return 0;
}

ptr = (char *)(&num);

printf(\"Hexadecimal value of %d is 0x%x\ \", num, num);

/* Scanning 4 byte data - byte by byte */
printf(\"Data in memory: \");
for (i = sizeof(int) - 1; i >= 0; i--) {
val = *(ptr + i);
printf(\"|0x%x\", val);
}
printf(\"|\ \");

/* data in first byte */
val = *ptr;

/* print the result */
if (!val) {
printf(\"Your Machine is Little Indian!!\ \");
} else {
printf(\"Your Machine is Big Indian!!\ \");
}

return 0;
}

C programming Consider this code. typedef union{ int n; unsigned char bytes [4]; }MyUnion; MyUnion u = { .bytes = { 0x57, 0x68, 0x6f, 0x00 } }; printf(\
C programming Consider this code. typedef union{ int n; unsigned char bytes [4]; }MyUnion; MyUnion u = { .bytes = { 0x57, 0x68, 0x6f, 0x00 } }; printf(\
C programming Consider this code. typedef union{ int n; unsigned char bytes [4]; }MyUnion; MyUnion u = { .bytes = { 0x57, 0x68, 0x6f, 0x00 } }; printf(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site