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
Prompt
madam [21]

Have you ever tried to learn a new language or do you have friends who've had that experience?

Yes, I tried to learn Python and I even managed to do two Discord bots which are (somewhat) functional, but I'm far to say that I've managed to lean the language completly. There are lots of things to learn on a language as Python.

I also leant PHP on my own and managed to do a website (somehow) functional.

What are some of the  steps you would take to learn a new language, and what are some challenges that might arise?

The first steps in learning any computer language is learning the syntax. If you manage to do that, with the experience you gained from previous projects/languages you might be able to create something working. At times you might feel down if the project doesn't work as expected.

What are some things that  can help make the process easier?

Video tutorials, experiments and searching questions and problems on Google is a very important resource.

3 0
2 years ago
Choose the correct term to complete the sentence
notka56 [123]

Answer:

popleft

Explanation:

5 0
3 years ago
Heyyy ik this isn't a dating site but like is there any guy (12,13,14)that would be willing to date a 13 y/o girl
Alexxx [7]
Lol I alit of people will be interested but we need to see proof that you are a girl but like just your face to know your not a catfish
3 0
3 years ago
Read 2 more answers
Is a ROUTER required for Internet Connectivity ?<br> Yes<br> No
Kazeer [188]

no it is not but u will need a ethernet cord

5 0
2 years ago
Which of the following assessments uses computer-adaptive technology to provide questions? SAT, ACT, ASVAB, or ACCUPLACER?
never [62]
ACCUPLACER, I hope this helps :)
7 0
3 years ago
Read 2 more answers
Other questions:
  • Judy forgot where she saved a certain file on her computer. Therefor, she searches for all files with a jpg file extension. Whic
    11·2 answers
  • In 1–2 sentences describe how you would insert a row in a spreadsheet.
    6·2 answers
  • What aspect of the internet makes it fault-tolerant?
    6·1 answer
  • Enterprise application integration (eai) software enables users to model the business processes and interactions that should occ
    8·1 answer
  • What kind of value should an employee possess when employees are expected to be responsible and fair?
    7·1 answer
  • Implement a recursive method named power that takes 2 integer parameters named base and expo. The method will return the base ra
    6·1 answer
  • Which of the following keywords is used to remove a row in a table? (Points : 2) DROP
    11·1 answer
  • Can someone help me with this
    10·1 answer
  • Can I get the code for the Edhesive Assignment 3 Chatbox in python? Thanks.
    15·1 answer
  • Identify and explain 3 methods of automatically formatting documents​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!