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
2. Name the layer of the Web/Internet Protocol Stack with which each of these functions is associated.
Nadya [2.5K]

Answer:

a. Encrypt a message - Application layer

b. Prevent buffer overruns - Transport Layer

c. Choose the best route for a data packet - Network Layer

d. Compose an email message - Application layer

Explanation:

3 0
3 years ago
Read 2 more answers
Consider designing a program where you need to store information about every student ASU. You need to be able to quickly determi
gulaghasi [49]

Answer:

Check the explanation

Explanation:

Both Arrays and Linked List can be used to store linear data of similar types, but they both have some advantages and disadvantages over each other. To store information about every student we can use an array

Reason:

In the array the elements belong to indexes, i.e., if you want to get into the fourth element you have to write the variable name with its index or location within the square bracket.

In a linked list though, you have to start from the head and work your way through until you get to the fourth element.

Accessing an element in an array is fast, while Linked list takes linear time, so it is quite a bit slower.

Example: store the data of students and have faster access and this program in example also do sorting

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// struct person with 3 fields

struct Student {

   char* name;

   int id;

   char age;

};

// setting up rules for comparison

// to sort the students based on names

int comparator(const void* p, const void* q)

{

   return strcmp(((struct Student*)p)->name,

                 ((struct Student*)q)->name);

}

// Driver program

int main()

{

   int i = 0, n = 5;

   struct Student arr[n];

   // Get the students data

   arr[0].id = 1;

   arr[0].name = "bd";

   arr[0].age = 12;

   arr[1].id = 2;

   arr[1].name = "ba";

   arr[1].age = 10;

   arr[2].id = 3;

   arr[2].name = "bc";

   arr[2].age = 8;

   arr[3].id = 4;

   arr[3].name = "aaz";

   arr[3].age = 9;

   arr[4].id = 5;

   arr[4].name = "az";

   arr[4].age = 10;

   // Print the Unsorted Structure

   printf("Unsorted Student Records:\n");

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

       printf("Id = %d, Name = %s, Age = %d \n",

              arr[i].id, arr[i].name, arr[i].age);

   }

   // Sort the structure

   // based on the specified comparator

   qsort(arr, n, sizeof(struct Student), comparator);

   // Print the Sorted Structure

   printf("\n\nStudent Records sorted by Name:\n");

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

       printf("Id = %d, Name = %s, Age = %d \n",

              arr[i].id, arr[i].name, arr[i].age);

   }

   return 0;

}

Output:

Unsorted Student Records: Id = 1, Name = bd, Age = 12 Id = 2, Name = ba, Age = 10 Id = 3, Name = bc, Age = 8 Id = 4, Name = aaz, Age = 9 Id = 5, Name = az, Age = 10

6 0
4 years ago
Pls help!!!!! I need these questions
g100num [7]

Answer:

online identity for the first one and none of the above for the second one:)

8 0
3 years ago
Read 2 more answers
You've received an assortment of files along with accompanying hashes to guarantee integrity. Some of the hash values are 256-bi
Ad libitum [116K]

Assuming they all use the same basic algorithm, what might it be SHA-1.

c. SHA-1

<u>Explanation:</u>

As solution hash value for system will used only 160 bit even though performance is bit slower. Normally SHA-1 method been used. Basically there four methods

MD5 is a methods where it checked regularly whether the file system or altered or not. RIPEMD is a method where it store or converts value to 160 bits. SHA1 methods following some security algorithms where it keeps bits in 160 bits with some security crypto or signature

SHA2- this method following some security algorithms where it keeps bit in 256 bits and we have sha384 and sha512. SHA 2 method is not used now days.

3 0
3 years ago
Candice’s first job was at the grocery store making deli food. While in culinary school, she worked part time in a restaurant ki
Anastasy [175]
<span>Candice’s first job was at the grocery store making deli food. While in culinary school, she worked part time in a restaurant kitchen, and after finishing school, she found a position as a head chef. With her growing experience and advanced education, Candice has been able to move up the career ladder.
As you can see, she started out making deli food, which is an okay job, but nothing spectacular. As she studied more and improved, she became a head chef, which is a great job and definitely better than her first one.
</span>
7 0
4 years ago
Read 2 more answers
Other questions:
  • What are the two basic steps in communication
    9·1 answer
  • Which part of the computer stores and processes data?
    14·1 answer
  • Write a C function which mimics the behavior of the assembly language function below. Note that this time, the assembly language
    10·1 answer
  • One or more access points positioned on a ceiling, wall, or other strategic spot in a public place to provide maximum wireless c
    9·1 answer
  • Each professional association has
    7·1 answer
  • True or False: Assuming that all variables are defined, the following segment of code is invalid. if ( x &gt; 0 )
    10·1 answer
  • Wake up to reality. Nothing ever goes as planned in this world. The longer you live,the more you realize that only pain, sufferi
    5·1 answer
  • We can save our data peremently on a
    7·2 answers
  • What value will the variable x have when the loop executes for the first time?
    11·1 answer
  • Your client Mr. Smith has requested a reservation at Eleven Madison Park this evening for a party of 2 at 6PM. Unfortunately, th
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!