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
Software, also called a(n) ____________________, consists of a series of related instructions, organized for a common purpose, t
Naddika [18.5K]
Software is also called a program or an application.
8 0
3 years ago
In Microsoft Exel, graphs are refered to as_________
algol [13]
The answer is charts D since tables are still referred to as tables
4 0
2 years ago
Leslie Is doing a sociological study of people who have live remote island. She interviews the people and than she observes them
dimulka [17.4K]
Making guesses, as opposed to the other answers, this is the only one that forges new ideas and doing such will only lead to an illegitimate conclusion/study.
5 0
2 years ago
Read 2 more answers
What is the output?
Aleksandr-060686 [28]

Answer:

The output of code is 20.

Explanation:

We need to give output of the following code:

numB = 25

while numB > 13:

numB = numB - 5

print(numB)

Solution:

We will check the condition of while loop, if the condition is true, then the statements inside the loop will be executed. Since brackets are not available so, only one statement is linked with while loop.

Executing the statements, we check condition while numB > 13 (25>13) is true, next statement will be executed numB = numB - 5  (20=25-13) so, when print(numB) will be executed, we will get 20.

The output of code is 20.

4 0
2 years ago
Read 2 more answers
When sending emails and setting goals, you want to go beyond checkpoints of activities for your goals. What are examples of chec
yulyashka [42]

Answer:

Open and click rates

Explanation:

Open and click rates are measuring units of how well your campaigns perform.

8 0
3 years ago
Other questions:
  • The part of the computer that contains the brain, or central processing unit, is also known as the A. monitor. B. keyboard. C. m
    13·2 answers
  • What are you required to verify before allowing a person to operate your pwc?
    8·1 answer
  • What is a mortgage?
    8·2 answers
  • What best describes deflation?
    9·2 answers
  • What does the term hardware refer to?
    13·1 answer
  • 1. Some of the music in the 1960s was used to protest social and political issues. Is music still used as a form of protest? Why
    13·1 answer
  • Sarah's Texas location has a server that is starting to give her trouble. It is needing to be restarted frequently and seems to
    15·1 answer
  • Which factor is NOT used to determine who can be let go during a downsizing?
    8·2 answers
  • When a computer is infected by a virus, _______.
    14·1 answer
  • ________ technologies are technologies that enable the incremental improvement of products and services.
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!