Created
May 16, 2020 06:30
-
-
Save gopi487krishna/7a9921750cdf7b9ac68c8f73f544df36 to your computer and use it in GitHub Desktop.
Convert Endianess associated with a number ( To Little Endian)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Task 3 : | |
This task is pretty simple guys . You are required to change the byte ordering of a number i.e you | |
have to convert the number to little-endian order . ( Read below for an example ) | |
Your task is to define the function cnv_to_littleendian such that it converts the byte ordering of the specified argument | |
and returns the number in little-endian format | |
Example ( Hexadecimal input,output ) | |
Input : 12345678 | |
Output: 78563412 | |
Explanation | |
Input : ( MSB to LSB ) | |
First Byte 12 | |
Second Byte 34 | |
Third Byte 56 | |
Fourth Byte 78 | |
Output : ( MSB to LSB ) | |
First Byte 78 | |
Second Byte 56 | |
Third Byte 34 | |
Fourth Byte 12 | |
Hint : | |
1. Read about Endianness from Wikipedia : https://en.wikipedia.org/wiki/Endianness | |
2. Byte Shuffling | |
3. Type Punning in C++ and C | |
There are multiple ways of doing this in C++. Although using type punning to do this task leads to undefined behaviour in C++ | |
sometimes, solutions using Type Punning will still be accepted. Other solutions also exist which are comparable in performance. | |
Note : | |
Although std::chrono has been used here for getting the timings ( how fast your function gets executed ) I highly suggest not to | |
use std::chrono for Practical Applications. This is because the results of std::chrono are not stastically stable. Instead i would | |
suggest you to use some benchmarking framework for that purpose ( Although using that here would have compilcated the stuff ) | |
*/ | |
#include<iostream> | |
#include<cstdint> | |
#include<cstring> | |
#include<chrono> | |
// Define this function please | |
std::uint32_t cnv_to_littleendian (std::uint32_t number){ | |
} | |
int main(){ | |
// YOU ARE NOT ALLOWED TO MAKE ANY MODIFICATIONS IN MAIN FUNCTION | |
// 32-bit integer to be taken from user ( unsigned int can also be used but size is not platform independent ) | |
std::uint32_t number; | |
/// Accepts the 32-bit Hexadecimal integer from the user | |
std::cin>>std::hex>>number; | |
// Time measurement ( Starts measuring from here) | |
auto start= std::chrono::high_resolution_clock::now(); | |
/// Converts to littleendian | |
auto result = cnv_to_littleendian(number); | |
/// Time measurement( Stops measuring) | |
auto end = std::chrono::high_resolution_clock::now(); | |
// Calculates how much time your function took | |
auto time = std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count(); | |
// Prints the time | |
std::cout<<"Timing :"<<time<<"\n"; | |
// Prints the converted number | |
std::cout<<std::hex<<result<<"\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment