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
Universal Containers needs the ability to generate contract documents. All the data required for a contract resides in a custom
Fittoniya [83]

Answer: (D) Select and install an App Exchange product to meet the contract generation needs.

Explanation:

The App exchange is the type of salesforce market place in the cloud platform and it included various types of cloud application and the services. We can select and then install the app exchange different types of product and services to meets the given generation needs.

The app exchange service is mainly used in the universal container that mainly needs for generating the document in the system. The user can able to install and access this type of app salesforce easily.

Therefore, Option (D) is correct.

 

6 0
2 years ago
Im bored anyone up for a convo? lets talk.. =D
aleksandrvk [35]

Answer:

ok im fine with dat <3

6 0
2 years ago
Read 2 more answers
The director of HR realizes that the KSAs currently used for hiring entry-level engineers are outdated. In order to establish wh
Vaselesa [24]

Answer:

Job analysis

Explanation:

The HR should preferably use job analysis.

6 0
3 years ago
the two main ways in which marketers address the competition with their strategies are by satisfying a need better than a compet
Nonamiya [84]

Offering a lower price (Apex)

4 0
2 years ago
Read 2 more answers
How can the Internet help our country to be a leader in technology?
BigorU [14]

Answer:

it can help in the aspect of making technologies to be fastly operated with fast internet connection there by it top among all other countries

4 0
2 years ago
Other questions:
  • A technician is troubleshooting a computer that will not communicate with any hosts on the local network. While performing a vis
    9·1 answer
  • Sam has installed a new CPU in a client’s computer, but nothing happens when he pushes the power button on the case. The LED on
    14·1 answer
  • Which key is used in word processors to create indentations?
    13·2 answers
  • Write a program which simulate rolling dice. When the program runs, it will prompt the user to choose a number ranging from 1 to
    15·1 answer
  • You want to make the background of a Web page blue, click _____.
    13·2 answers
  • A microsoft windows os component responsible for representing graphical objects and transmitting them to output devices such as
    15·1 answer
  • Three reasons Why we connect speakers to computers
    15·2 answers
  • Why are computers better than humans at solving a problem like finding the shortest route between ten different cities?
    15·2 answers
  • Describe from an administrator perspective what is necessary to prepare a storage device for access by a user.
    14·1 answer
  • Which of the following is an operating system?<br> MacBook Air<br> Windows 10<br> Dell
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!