Program Discussion :: Bitwise Operator
19 / 15
Write an efficient program to convert a given number from little endian to big endian
Answer:
#include
#include
using namespace std;
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n)
{
int i;
for (i = 0; i < n; i++)
printf(" %.2x", start[i]);
printf("\n");
}
/*Main function to call above function for 0x01234567*/
int main()
{
int i = 0x01234567; show_mem_rep((char *)&i, sizeof(i));
getchar();
return 0;
}
Asked In ::
Language:
Shreya
7 Jul, 2017 9:30 AM
#include <stdio.h>
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n)
{
int i;
for (i = 0; i < n; i++)
printf(" %.2x", start[i]);
printf("\n");
}
/*Main function to call above function for 0x01234567*/
int main()
{
int i = 0x01234567;
show_mem_rep((char *)&i, sizeof(i));
getchar();
return 0;
}
Language:
Shreya
7 Jul, 2017 9:30 AM
#include <stdio.h>
int convert(int);
int main()
{
int dec, bin;
printf("Enter a decimal number: ");
scanf("%d", &dec);
bin = convert(dec);
printf("The binary equivalent of %d is %d.\n", dec, bin);
return 0;
}
int convert(int dec)
{
if (dec == 0)
{
return 0;
}
else
{
return (dec % 2 + 10 * convert(dec / 2));
}
}
Language:
Nikhil
7 Jul, 2017 9:30 AM
#include <stdio.h>
#include<iostream>
using namespace std;
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n)
{
int i;
for (i = 0; i < n; i++)
printf(" %.2x", start[i]);
printf("\n");
}
/*Main function to call above function for 0x01234567*/
int main()
{
int i = 0x01234567; show_mem_rep((char *)&i, sizeof(i));
getchar();
return 0;
}