The program will be read in c language with the following requirements for hexad
ID: 3665333 • Letter: T
Question
The program will be read in c language with the following requirements for hexadecimial, binary and decimal.
Please read eveyrthing here if you require more time to complete the following below please inform me and I will post it again and if you want I could even send you a email to let you know when I'll repost it again and schedule it with you that way.
1. Whenever possible you must make use of bit-wise operators (not arithmetic operators).
3. main routine: The main routine is to loop displaying a menu of options and then to perform the selected operation on an input integer number. The operations to be displayed / performed are the following:
• display number in designated base • exchange odd numbered bytes (1 with 3) • exchange even numbered bytes (0 with 2) • take the one’s complement of the number
Note for all operations, the final result is to be displayed in the designated base (see next item).
4. display routine: Write one function that takes an integer value as an input argument and then displays the number in the designated base. The base is either binary, decimal or hexadecimal. The selection of base is to be made at compile time via the –D option. Instead of having three separate routines, one for binary, one for decimal and one for hexadecimal, you are to write one routine some of whose statements vary based on the selection of base. This variation of statements is to be controlled via conditional pre-processor directives.
5. Exchange byte macros: Write and use two macros that perform the exchange operations:
. XODD(N): exchanges the odd numbered bytes with each other while leaving the content of the other bytes intact. It must be possible to use the macro as follows: n = XODD(n).
. XEVEN(N): exchanges the even numbered bytes with each other while leaving the content of the other bytes intact. It must be possible to use the macro as follows: n = XEVEN(n).
Note that these macro definitions must not call a subroutine; that is, the code must be “inline”. Also, the right most byte is to be considered byte 0 with the other bytes numbered sequentially from right to left.
6. Readability: Your program must be written using good C programming conventions:
• Variable names and function names should be descriptive of what they represent.
• Use indentation to show the structure of the program. Typically this means using indentation with for, while, do-while, if, and switch statements as well as indenting the body of functions. Indentation should show the nesting level of the statements involved.
• Include some in-line documentation to give a high level view of what the program and groups of statements is/are doing.
A sample of what the output should look like is as follows:
Sample output:
Input number: 857870592
Select option:
1 - display number in hexadecimal
2 - exchange odd bytes
3 - exchange even bytes
4 - take complement of number
5 - exit
1
number: 0x33221100
Select option:
1 - display number in hexadecimal
2 - exchange odd bytes
3 - exchange even bytes
4 - take complement of number
5 - exit
2
number before: 0x33221100
number after: 0x11223300
Select option:
1 - display number in binary
2 - exchange odd bytes
3 - exchange even bytes
4 - take complement of number
5 - exit
3
number before: 00110011001000100001000100000000
number after: 00110011000000000001000100100010
Select option:
1 - display number in binary
2 - exchange odd bytes
3 - exchange even bytes
4 - take complement of number
5 - exit
4
number before: 00110011000000000001000100100010
number after: 11001100111111111110111011011101
Explanation / Answer
#include <stdio.h>
void convertToHexadecimal(int n){
int decNumber=n;
int qtnt;
int i=1,k,temp;
char hexadecNumber[100];
qtnt=decNumber;
while(qtnt!=0){
temp=qtnt%16;
//convert the integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecNumber[i++]= temp;
qtnt/=16;
}
printf("Hexadecimal value: ");
for(k=i-1;k>0;k--)
printf("%c",hexadecNumber[k]);
}
void exchangeOddBytes(int n){
//all the even bits of n
unsigned int even_bits= n & 0xAAAAAAAA;
//all the odd bits of n
unsigned int odd_bits = n & 0x55555555;
even_bits>>=1;//Right shift all the even bits
odd_bits<<=1;//Left shift all the odd bits
printf("%u",even_bits | odd_bits); //Combine all the even and odd bits
}
void exchangeEvenBytes(int n){
//all the odd bits of n
unsigned int odd_bits = n & 0x55555555;
//all the even bits of n
unsigned int even_bits= n & 0xAAAAAAAA;
odd_bits<<=1;//Left shift all the odd bits
even_bits>>=1;//Right shift all the even bits
printf("%u",odd_bits | even_bits); //Combine all the even and odd bits
}
void takeComplement(int n){
int remainder;
if (n<= 1)
{
printf("%d",!n);
return;
}
remainder=n%2;
takeComplement(n/2);
printf("%d",!remainder);
}
int main(void) {
printf("1 - display number in hexadecimal ");
printf("2 - exchange odd bytes ");
printf("3 - exchange even bytes ");
printf("4 - take complement of number ");
printf("5 - exit ");
int num;
printf("Enter a number:");
scanf("%d",&num);
printf("Enter choice:");
int ch;
scanf("%d",&ch);
switch(ch){
case 1:
convertToHexadecimal(num);
break;
case 2:
exchangeOddBytes(num);
break;
case 3:
exchangeEvenBytes(num);
break;
case 4:
takeComplement(num);
break;
default:
printf("Invalid choice!");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.