The program will be read in c language with the following requirements for hexad
ID: 3665254 • 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
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.
// Here let type be the desingated base- so let 1 be binary, 2 be hexadecimal.
void changeBase(int integer,int type)
{
if(type==2)
{
char hex[5];
sprintf(hex, "%x", integer);
puts(hex);
}
else if(type==1)
{
int binaryNumber[100];
int binaryNumber2[100];
int k=0;
while(integer!=0){
binaryNumber[i++]= integer % 2;
integer = integer / 2;
}
for(j = i -1 ;j> 0;j--)
{
binaryNumber2[k]=binaryNumber[j];
k++;
}
}
}
To exchange digits, you can use the following script
unsigned int exchange(unsigned int x)
{
unsigned int even = x & 0xAAAAAAAA;
unsigned int odd = x & 0x55555555;
even >>= 1;
odd <<= 1;
return (even | odd);
}
I have few doubts, please help me. You want to change from each base to other base. For instance , from decimal to hexa and binary. and from binary to hexa and decimal, and from hexa to
binary and decimal.
Secondly, for swapping- please tell if this script would suffice, or do you need anything more on it. Please reply :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.