You are on page 1of 12

Networks Laboratory (CN Lab.

)
[IT-3095]
Individual Work
Lab. No: 02 Topic: BIG ENDIAN AND LITTLE ENDIAN
Date: 26/07/22
Roll Number: 2029190 Branch/Section: CSCE-2
Name in Capital: ALAPAN PRADHAN

Program No: 2.1

Program Title:
Write a program to extract the first byte and last byte of an unsigned integer, input with command line arguments.

Input/Output
Screenshots: RUN-1:

RUN-2:
Source code:
#include <stdio.h>
int getByte(int num, unsigned byteno)
{
return (num>>(byteno * 8)) & 0xFF;
}

int main()
{
int num;
unsigned byteno;

printf("\nEnter a hex number: ");


scanf("%x", &num);
printf("\nEnter byte no to get (0 - 3): ");
scanf("%u", &byteno);
printf("\nByte no %u in hex representation: %x\n", byteno, getByte(num, byteno));
printf("\nEnter byte no to get (0 - 3): ");
scanf("%u", &byteno);

printf("\nByte no %u in hex representation: %x\n", byteno, getByte(num, byteno));


return 0;
}

Conclusion/Observation:
We observed how to extract the first byte and last byte of an unsigned integer, when inputted with
command line arguments.
Program no. : 2.2

Program Title:

Write a program to extract each byte from a given number and store them in separate character variables and print
the content of those variables.

Input/Output
Screenshots:

RUN-1:

Run2:
Source Code:

#include <stdio.h>

typedef unsigned char BYTE;

int main()
{
unsigned int value=0x12345678;

BYTE a,b,c,d;

a=(value&0xFF);
b=((value>>8)&0xFF);
c=((value>>16)&0xFF);
d=((value>>24)&0xFF);

printf("a= %02X\n",a);
printf("b= %02X\n",b);
printf("c= %02X\n",c);
printf("d= %02X\n",d);
return 0;
}

Program no. : 2.3

Program Title: Write a program to find out if the underlying architecture/machine is little endian or big endian.

Input/Output
Screenshots:
RUN-1:

Source Code:

#include <stdio.h>

int main ()

unsigned int x = 0x76543223;

char *c = (char*) &x;

printf ("*c is: 0x%x\n", *c);

if (*c == 0x10)

{
printf ("Underlying architecture is little endian. \n");

else

printf ("Underlying architecture is big endian. \n");

return 0;

Program no. : 2.4

Program Title: Write a program to extract the first byte and last byte of an unsigned integer, input with keyboard.
If first byte and last byte are same, then it will ask the user to input a new interger again. This process continue till
both bytes are same. If both are not same then it will print the message as “Succeeded with 2 attempts” where 2 is
the number of entering the unsigned number through keyboard.

Input/Output
Screenshots:
RUN-1:

RUN-2:
Source Code:

#include <stdio.h>
int getByte(int num, unsigned byteno0,unsigned byteno1)
{
unsigned a,b;
a=(num >> (byteno0 * 8)) & 0xFF;
b= (num >> (byteno1 * 8)) & 0xFF;
return a,b;
}

int main()
{
int num;
unsigned byteno0,byteno1;

printf("\nEnter a int number: ");


scanf("%x", &num);
printf("\nEnter byte no to get (0 - 3): ");
scanf("%u%u", &byteno0,&byteno1);
printf("\nByte no 0 and 3 in hex representation: %x %x\n",
getByte(num,byteno0,byteno1));
if(byteno0==byteno1)
printf("Enter a new integer");
else
printf("Succeeded with 2 attempts");

return 0;
}
Program no. : 2.5

Program Title:

Write a program to extract each byte as character from the representation of multibyte data types on your machine
and its corresponding memory locations. By studying the data in memory, findout if the underlying
architecture/machine is little endian or big endian.

Input/Output
Screenshots:
RUN-1:

RUN-2:

Source Code:

#include <stdio.h>

typedef unsigned char BYTE;

int main()
{
unsigned int value=0x236743;

BYTE a,b,c,d;
a=(value&0xFF);
b=((value>>8)&0xFF);
c=((value>>16)&0xFF);
d=((value>>24)&0xFF);

printf("a= %02X\n",a);
printf("b= %02X\n",b);
printf("c= %02X\n",c);
printf("d= %02X\n",d);
char *f = (char*) &a;

printf ("*c is: 0x%x\n", *f);


if (*f == 0x10)
{
printf ("Underlying architecture is little endian. \n");
}
else
{
printf ("Underlying architecture is big endian. \n");
}

return 0;
}

Program no. : 2.6

Program Title: Write a C program to convert Littel Endian to Big Endian format by using a suitable user

defined function for it.

Input/Output
Screenshots:
RUN-1:

RUN-2:
Source Code:

#include <stdio.h>

unsigned int LitToBigEndian(unsigned int x);

int main( )
{
int Little_Endian = 0x45678921 ;

printf("\n Little_Endian = 0x%X\n",Little_Endian);

printf("\n Big_Endian = 0x%X\n",LitToBigEndian(Little_Endian));

unsigned int LitToBigEndian(unsigned int x)


{
return (((x>>24) & 0x000000ff) | ((x>>8) & 0x0000ff00) | ((x<<8) & 0x00ff0000) |
((x<<24) & 0xff000000));
}

Program no. : 2.7

Program Title: Write a program to extract each byte as character from the representation of multibyte data types
on your machine and store the above extracted bytes with the following structure. Now display the contains of
structure members.

struct pkt
{
char ch1;
char ch2[2];
char ch3;
}

Input/Output
Screenshots:
RUN-1:
Source Code:

#include <stdio.h>

#include <stdint.h>

union my_int {

int val;

uint8_t bytes[sizeof(int)];

};

int main(int argc, char** argv) {

union my_int mi;

int idx;

mi.val = 1234567;

for (idx = 0; idx < sizeof(int); idx++)

printf("byte %d = %hhu\n", idx, mi.bytes[idx]);

return 0;

Program no. : 2.8

Program Title: Write a program to modify the above program to regenerate the original number from the structure
members. Display the number.

Input/Output Screenshots:

RUN-1: RUN-2:
Source Code:

#include <stdio.h>
#include <stdint.h>

union my_int {
int val;
uint8_t bytes[sizeof(int)];
};

int main(int argc, char** argv) {


union my_int mi;
int idx;

mi.val = 2345566;

for (idx = 0; idx < sizeof(int); idx++)


printf("byte %d = %hhu\n", idx, mi.bytes[idx]);
printf("%d",mi.val);

return 0;
}

Program no. : 2.9

Program Title: Write a program to extract each byte from an signed integer and display the content of
each location in decimal.

Input/Output
Screenshots:
RUN-1:

RUN-2:
Source Code:

#include <iostream>
#include <stdio.h>
#include <limits.h>
using namespace std;

void print(unsigned char byte)


{
for (int i = 7; i >= 0; i--) {
int b = byte >> i;
if (b & 1)
cout << "1";
else
cout << "0";
}
}

int main(){
unsigned char bytes[4];
unsigned long n = 123456;

bytes[0] = (n >> 24) & 0xFF;


bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> 8) & 0xFF;
bytes[3] = (n >> 0) & 0xFF;

cout << "Integer: " << n << endl;

cout << "Byte Array: ";

for(int i = 0; i < 4; i++){


print(bytes[i]);
cout << "\t";
}
}

You might also like