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]
3 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]3 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
The rhythmic note that three beats is called a____half note.
aev [14]

Answer:

it is called a dotted half note

7 0
3 years ago
Read 2 more answers
Which is an example of correct HTML? &lt;h1&gt; This is a heading&lt;/h1&gt;
denis23 [38]

Answer:

An HTML tag is a special word or letter surrounded by angle brackets, < and >. You use tags to create HTML elements , such as paragraphs or links.

Explanation:

Many elements have an opening tag and a closing tag — for example, a p (paragraph) element has a <p> tag, followed by the paragraph text, followed by a closing </p> tag. HTML Editors

Step 1: Open Notepad (PC) Windows 8 or later: ...

Step 1: Open TextEdit (Mac) Open Finder > Applications > TextEdit. ...

Step 2: Write Some HTML. Write or copy the following HTML code into Notepad: ...

Step 3: Save the HTML Page. Save the file on your computer. ...

Step 4: View the HTML Page in Your Browser. hope this helps you :) god loves you :)

4 0
3 years ago
Read 2 more answers
Which actions changed the look of the following word?
jarptica [38.1K]

Answer:

D

Explanation:

The actions applied to change the look of the word are:

1. It was set to italics. This can be done easily by highlighting the word => press Ctrl + I together on the keyboard.

2. Next is to set to underline. While still highlighted => press Ctrl + U together in the keyboard.

6 0
3 years ago
Read 2 more answers
Many organizations find themselves in the position of being data rich and information poor. Even in today's electronic world, ma
juin [17]

Answer:True

Explanation:

Many organizations find themselves in the position of being data rich and information poor. Even in today's electronic world, managers struggle with the challenge of turning their business data into business intelligence.Data in its raw form does not help managers in reaching their business decisions. In this electronic age data collection has been made simple .However the value of that data can only be seen when it is processed and it becomes information. It can help managers to make quick business decisions that will be used to make come up with important business strategies  .It follows that when data is collected in its various forms it should then be processed meaning that the business managers can either use business Intelligent software  that can present the data in a meaningful form like graphs . pie charts and other forms that can be easily interpreted and reflect the trends that have been presented by the raw data .The organisation should find value in the data that they have collected and tell the story that leaves in the data.

7 0
3 years ago
Two people, Alice and Bob are using the key sharing algorithm. They have chosen a clock size of 17 and a base of 5. Alice's priv
hjlf

Answer:

fsfsf

Explanation:

6 0
3 years ago
Other questions:
  • What can search the Internet and select elements based on important words?
    15·2 answers
  • Pls help............
    9·2 answers
  • A(n) ____ database is an application appropriate for an object-oriented database that contains text links to other types of docu
    7·1 answer
  • Is there any level after genius?
    8·1 answer
  • Write a Java program to accept an item's name and price from the user and output them to the console
    5·1 answer
  • What is the advantage of saving a document as a PDF file?
    6·1 answer
  • What is the purpose of a Macro in Word?: *
    13·1 answer
  • In the GO programming language you can't declare a float, it must be a float32 or float64. Why do you think this is done if the
    10·1 answer
  • When the function below is called with 1 dependent and $400 as grossPay, what value is returned?
    13·1 answer
  • Pls answer dis question when i'm not connected and i open bluestacks 5 it will be working but if i am connected it wil not work
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!