Question

2. Write a C code with a function to swap the first four bits of an 8-bit unsigned number with the last four bits. The main function should include the variable declaration and initialisation using the appropriate data type, the function call and the display of the result in hexadecimal. The parameter(s) should be passed to the function by reference.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following C code gives you the required functionality:

#include<stdio.h>
void swap(int *n);

int main()
{
unsigned char x;
printf("Enter the value: ");
scanf("%u",&x);
swap(&x);
return 0;
}//end of main function

void swap(int *n)
{
unsigned char d = *n;
char s[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
char hex[2];
int j=1,res,i;
for(i=0;i<2;i++)
hex[i]='0';


//converting into hexadecimal values
while(d!=0)
{
res = d%16;
if(res<=9)
hex[j--]=res;
else
hex[j--]=s[res-10];
d=d/16;
}
printf("Before Swapping: 0x");
for(i=0;i<2;i++)
printf("%c",hex[i]);
printf("\n");
char t;
t = hex[0];
hex[0]=hex[1];
hex[1]=t;
printf("After Swapping: 0x");
for(i=0;i<2;i++)
printf("%c",hex[i]);

}//end of swap function

Output:

Enter the value: 12 Before Swapping: exec After Swapping: exce Process returned (8x0) execution time : 1.703 s Press any key

Add a comment
Know the answer?
Add Answer to:
2. Write a C code with a function to swap the first four bits of an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...

    C++ Code Pass By Reference Practice Write the following functions for basic array operations based on the function descriptions given below. Write the following functions for basic array operations based on the function descriptions given below. FindMinArray: This function will search an array of integers (passed to the function as a parameter) for its minimum value, then return that value. SumArray: This function will sum an array of integers (passed to the function through a parameter) then return that sum....

  • C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void...

    C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void prime (int) The number of primes to display is passed as the parameter. The function prime itself is not recursive. However, it should call a separate recursive helper function which determines if a given number is prime #include <iostream> using namespace std; void prime(int ) { } int main() {    prime (21);    return 0; }

  • pick two answers 12 points). The function swap() should swap two integers and main() should print...

    pick two answers 12 points). The function swap() should swap two integers and main() should print those two swapped integers. What (if anything is wrong with this code? Select all that apply. oooo. No ure W..Ni def main(): X = 10 y = 2 x, y = swap(x,y). print(x,y) def swap(a,b): a = b temp = a b = temp main N on line 9, it is illegal syntax to introduce a new variable named temp. Instead, temp should be...

  • Complete a partially written C++ program that includes a function that return a value. The program...

    Complete a partially written C++ program that includes a function that return a value. The program is a simple calculator that prompts the user of 2 number and an operation (+, -, * or, /). The two number and the operator are passed to the function where the appropriate arithmetic operation is performed. The result is return to the main function () where the arithmetic operation and result are displayed. For example 3 * 4 = 12 The source code...

  • Please write the code in VHDL 1. An entity named reorder has an 8-bit std_logic_vector input...

    Please write the code in VHDL 1. An entity named reorder has an 8-bit std_logic_vector input and an 8-bit std logic_ vector output. The bits of the entity's output vector have the reverse order of the bits of its input vector. The architecture must use a single concurrent call to a function The function, named reorder_vec, is defined in the declaration section and returns a std_logic vector whose bits have the reverse order of the bits in the std_logic vector...

  • Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and...

    Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and y and returns the unsigned 64 bit integer sum z, i.e. zx+y. In your main function, you should assume that x, y, and z will be stored in the following 6 registers as follows: x: upper 32 bits in $t1 y: upper 32 bits in St3 z: upper 32 bits in St5 lower 32 bits in $to lower 32 bits in $t2 lower 32...

  • Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x....

    Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x. Assume w=32. * For example 0xFF00 -> 0x8000, and 0x6600 --> 0x4000. * If x = 0,then return 0. */ int leftmost_one(unsigned x); Your function should follow the above bit-level integer coding rules, except that you may assume that data type int has w=32 bits. Your code should contain a total of at most 15 arithmetic, bit-wise, and logical operations. In C++ and has...

  • Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The...

    Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The functions are described below on page 2. (Just write the prototypes) When necessary, use the variables declared below in maino. mm 1.) showMenu m2.) getChoice 3.) calcResult m.) showResult 5.) getInfo mm.) showName 7.) calcSquare 8.) ispositive int main { USE THESE VARIABLES, when needed, to write function prototypes (#1 - #8) double num1 = 1.5; double num2 = 2.5; char choice; double result;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT