1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nataliya [291]
2 years ago
15

Write a C program that reads two hexadecimal values from the keyboard and then stores the two values into two variables of type

unsigned char. Read two int values p and k from the keyboard, where the values are less than 8. Replace the n bits of the first variable starting at position p with the last n bits of the second variable. The rest of the bits of the first variable remain unchanged. Display the resulting value of the first variable using printf %x.Test Cases:n = 3;p=4;input: a= 0x1f (0001 1111) b = c3 (1100 0011); output: f (0000 1111)n = 2;p=5;input: a= 0x1f (0001 1111) b = c3 (1100 0011); output: 3f (0011 1111)
Computers and Technology
1 answer:
sattari [20]2 years ago
4 0

Solution :

#include  $$

#include $$

#include $$

//Converts $\text{hex string}$ to binary string.

$\text{char}$ * hexadecimal$\text{To}$Binary(char* hexdec)

{

 

long $\text{int i}$ = 0;

char *string = $(\text{char}^ *) \ \text{malloc}$(sizeof(char) * 9);

while (hexdec[i]) {

//Simply assign binary string for each hex char.

switch (hexdec[i]) {

$\text{case '0'}:$

strcat(string, "0000");

break;

$\text{case '1'}:$

strcat(string, "0001");

break;

$\text{case '2'}:$

strcat(string, "0010");

break;

$\text{case '3'}:$

strcat(string, "0011");

break;

$\text{case '4'}:$

strcat(string, "0100");

break;

$\text{case '5'}:$

strcat(string, "0101");

break;

$\text{case '6'}:$

strcat(string, "0110");

break;

$\text{case '7'}:$

strcat(string, "0111");

break;

$\text{case '8'}:$

strcat(string, "1000");

break;

$\text{case '9'}:$

strcat(string, "1001");

break;

case 'A':

case 'a':

strcat(string, "1010");

break;

case 'B':

case 'b':

strcat(string, "1011");

break;

case 'C':

case 'c':

strcat(string, "1100");

break;

case 'D':

case 'd':

strcat(string, "1101");

break;

case 'E':

case 'e':

strcat(string, "1110");

break;

case 'F':

case 'f':

strcat(string, "1111");

break;

default:

printf("\nInvalid hexadecimal digit %c",

hexdec[i]);

string="-1" ;

}

i++;

}

return string;

}

 

int main()

{ //Take 2 strings

char *str1 =hexadecimalToBinary("FA") ;

char *str2 =hexadecimalToBinary("12") ;

//Input 2 numbers p and n.

int p,n;

scanf("%d",&p);

scanf("%d",&n);

//keep j as length of str2

int j=strlen(str2),i;

//Now replace n digits after p of str1

for(i=0;i<n;i++){

str1[p+i]=str2[j-1-i];

}

//Now, i have used c library strtol

long ans = strtol(str1, NULL, 2);

//print result.

printf("%lx",ans);

return 0;

}

You might be interested in
Which of the following would be the most appropriate way to address and greet your teacher, Mr. Joseph Herman, in an email? Hi J
Valentin [98]
To determine what would be the most appropriate way to address and greet your teacher during an email, we should eliminate some greetings, such as:
"Yo wassup?", "How  u doin?", or any other grammatical and socially inappropriate errors.
Let's look at our first option.
"Hi Joseph, How u doin??". This is incorrect as it is not appropriate to address anyone in such a manner and with grammatical errors.
Let's look at our second option.
"Dear Joseph Herman, how are you doing!!". This was on the right path, but didn't end well. The ending of the message, "how are you doing!!" is incorrect punctuation, and has too much excitement.
How about our third option?
"Dear Mr. Herman, I hope you're doing fine.". This is a great email. It has perfect punctuation, grammar, and is appropriate.
What about our fourth?
"Dear Joseph, i hope you are doing great.". This is a good email, but has incorrect punctuation.
Your answer is C.) Dear Mr. Herman, I hope you're doing fine.
8 0
3 years ago
Davids family took him to a hospital as he was suffering from a sericous ailment
marta [7]

Answer:

Here are some other sentences which always demonstrate that perhaps the hospital operates with either the help of technology, which is given below.

Explanation:

  • As eventually as he was acknowledged or confessed, the medical records of David would be documented by the medical practitioners.
  • David was therefore positioned through an ICU during which his heart rate had been monitored closely (those same types of machinery were directly linked to the desktops in position to obtain comment of his vital statistics.)
7 0
2 years ago
Using bearings will cause more friction. Group of answer choices True False
Olegator [25]

Answer:

False

Explanation:

6 0
3 years ago
Please help!!!
Alchen [17]

The most probable reason why your code for the dog turning right instead of is because your conditional statements are not satisfied.

<h3>What is Debugging?</h3>

This refers to the identification and removal of errors in a given code or hardware component.

Hence, we can see that you should note that you are using teh ifElse conditional statement to execute your code and you stated that the dog would turn right if:

  1. The left side is blocked
  2. If there are balls present.

Therefore, there are balls present and also the left side is blocked which causes your dog to turn right because it is executing the else statement.

Read more about debugging here:

brainly.com/question/16813327

#SPJ1

6 0
2 years ago
What is the best approach to handling the expectation of privacy by employees in the event an investigation needs to be carried
Wittaler [7]

Answer

The best approach to handling the expectation of privacy by employees in the event an investigation needs to be carried out on company-owned digital assets:-<u>A well-defined published policy that clearly states that an employer has the right to examine, inspect or access company-owned assets.</u>

3 0
2 years ago
Other questions:
  • A local area network works as a ____ network in that clusters of workstations are connected to a central point (hub or switch) t
    6·1 answer
  • nswer the following questions concerning chapter 1:1.1 Which pair of layers are NOT peer layers?a.Transport layer in the sender
    9·1 answer
  • Which of the following is a job description for a person with a degree in information technology
    15·1 answer
  • Write a code to define a structure named student, which includes name, student number and email address
    5·1 answer
  • In cell n2, enter a formula using the if function and a structured reference to determine if allison simoneau is eligible for tu
    11·1 answer
  • How to find out what windows version i have?
    10·1 answer
  • Short notes on Supply chain Management System (SCMS)
    12·1 answer
  • Which of the following statements best reflects the pros and cons of overtime potential for a line installer or repairer?
    15·1 answer
  • HELP FAST PLEASE
    6·1 answer
  • True or false. The send e-mail feature, listed under tools, will allow you to send an e-mail to your instructor and to fellow st
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!