Professional Documents
Culture Documents
)
[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 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;
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>
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 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 ()
if (*c == 0x10)
{
printf ("Underlying architecture is little endian. \n");
else
return 0;
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;
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>
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;
return 0;
}
Program Title: Write a C program to convert Littel Endian to Big Endian format by using a suitable user
Input/Output
Screenshots:
RUN-1:
RUN-2:
Source Code:
#include <stdio.h>
int main( )
{
int Little_Endian = 0x45678921 ;
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 idx;
mi.val = 1234567;
return 0;
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)];
};
mi.val = 2345566;
return 0;
}
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;
int main(){
unsigned char bytes[4];
unsigned long n = 123456;