Hello all!
This is my C program that is supposed to:
1. prompt user for integer
2. display integer in decimal
3. display binary value of integer
4. flip from little-endian to big-endian and display binary
5. display NEW big-endian in decimal
6. quit on user input 'q'.
First question: this program does not display the binary values properly.. where did I mess up?
Also:
Besides alternatives to scanf or printf, or using function calls to convert to binary, where can I improve my code? I'm looking for efficiency, easy implementation etc..
Thanks!!
Code:#include <stdio.h> int main(void) { while (1) { short i; short j; char input[40]; printf("Enter a 32-bit integer ('q' to quit): "); scanf("%s", input); if (input[0] == 'q') return 0; int number = atoi(input); char little[32]; for( i = 31; i >= 0; i--) { if( (1 << i) & number) little[31 - i] = '1'; else little[31 - i] = '0'; } char big[32]; for ( j = 4; j > 0; j--) { for ( i = 8; i > 0; i--) { big[(8 * j) - i] = little[(8 * (5 - j)) - i]; } } int flipped = 0; for( i = 31; i >= 0; i--) { if ( big[i] = '1') flipped = flipped + 2^i; } printf("\n32-bit integer: %d\nLittle-endian binary: %s\nBig-endian binary: %s\nNew 32-bit integer: %d\n\n",number,little,big,flipped); } return 0; }