Question

1) Write a C program that reads a hexadecimal value from the keyboard and then stores...

1) Write a C program that reads a hexadecimal value from the keyboard and then stores the value into an unsigned char variable. Read two int values p and n from the keyboard, where the values are less than 8. Implement the following commands: S – sets the n bits starting at position p to 11..1 R – resets the n bits starting at position p to 00…0 F – flips the n bits starting at position p to their inverse bit D – displays the value of the variable I – reads a new hexadecimal value from the keyboard

2) Repeat exercise 1 using a variable of type unsigned int. Discuss the differences between the two programs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

solution:

Following are the screenshots of code and output:

Part 1)

Part 2)

Or operation is used for setting ; And for resetting ; Xor for flipping.

The only difference between two programs is that the variable in second program(unsigned char) can store 32 bits while the variable in first program(unsigned int) can store only 8 bits.Thus in first program the maximum value of n & p can be 8 ,while in second program it can be upto 32.

C program for part 1

#include<stdio.h>

int main(){

//using unsigned char

unsigned char var;

int p,n;

//inputting values

printf("a = ");

scanf("%x",&var);

printf("p = ");

scanf("%d",&p);

printf("n = ");

scanf("%d",&n);

while(1){

char command;

scanf("%c",&command);

//implementing commands

if(command=='S'){

var=var | (((1<<n)-1)<<(p-1));

}

else if(command=='R'){

var=var & ~(((1<<n)-1)<<(p-1));

}

else if(command=='F'){

var=var ^ (((1<<n)-1)<<(p-1));

}

else if(command=='D'){

//displaying in hexadecimal

printf("a = %02x H : ",var);

int i;

//displaying in binary

for(i=7;i>=4;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=3;i>=0;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" B\n");

}

else if(command=='I'){

printf("a = ");

scanf("%x",&var);

}

}

}

C program for part 2

#include<stdio.h>

int main(){

//using unsigned int

unsigned int var;

int p,n;

//inputting values

printf("a = ");

scanf("%x",&var);

printf("p = ");

scanf("%d",&p);

printf("n = ");

scanf("%d",&n);

while(1){

char command;

scanf("%c",&command);

//implementing commands

if(command=='S'){

var=var | (((1<<n)-1)<<(p-1));

}

else if(command=='R'){

var=var & ~(((1<<n)-1)<<(p-1));

}

else if(command=='F'){

var=var ^ (((1<<n)-1)<<(p-1));

}

else if(command=='D'){

//displaying in hexadecimal

printf("a = %08x H : ",var);

int i;

//displaying in binary

for(i=31;i>=28;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=27;i>=24;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=23;i>=20;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=19;i>=16;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=15;i>=12;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=11;i>=8;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=7;i>=4;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" ");

for(i=3;i>=0;i--)

printf("%d",((1<<i)&(var))>>i);

printf(" B\n");

}

else if(command=='I'){

printf("a = ");

scanf("%x",&var);

}

}

}

please give me thumb up

Add a comment
Know the answer?
Add Answer to:
1) Write a C program that reads a hexadecimal value from the keyboard and then stores...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • Write a program in C that reads a string of bits( so either a one or...

    Write a program in C that reads a string of bits( so either a one or zero) in from the user one char at a time using the function getChar, which returns a char. hint in order to convert a char to an int, subtract the character. Then store the bits into an array. start with this: #include "stdio.h" #define MAX_BITS 32 int main() { printf("Enter up to 32 bits (hit 'enter' to terminate early): "); char bit = getchar();...

  • C PROGRAM When you print out the old and new bitsets, which are of type unsigned...

    C PROGRAM When you print out the old and new bitsets, which are of type unsigned char, please use the %p control character in the printff statement rather than %x or %d or %c. The compiler will complain, but we don't always listen to them anyway. The difference is it will print the bitset out in hex with a preceeding %0x. unsigned char bitset = 0x14 ; printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14, which is what...

  • 1. Write a program that reads a sequence of numbers from the keyboard, and displays the...

    1. Write a program that reads a sequence of numbers from the keyboard, and displays the smallest number. The sequence has at least one number and is terminated by -999(-999 will not appear in the sequence other than as the end marker). 2. Write a program which requests an integer input n from the keyboard and computes the sum of square of k for all k from 1 to n(inclusive). use java

  • Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code...

    Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...

  • using c++ Write a C++ program which reads three values of types char, int, double and...

    using c++ Write a C++ program which reads three values of types char, int, double and string from the keyboard and primis, appropriately formatted, assigned values and variable types. For example, if letter, number, and real are variables of type char, int and double respectively, and if the values assigned to them using cin function are: a, 1, and 3.1415, then the output should be: a is a character 1 is an integen 3.1415 is a real number

  • Write a complete program that declares an integer variable, reads a value from the keyboard into...

    Write a complete program thatdeclares an integer variable,reads a value from the keyboard into that variable, andwrites to standard output the square of the variable's value.

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • Write a C++ program that reads in 6 elements from the keyboard and stores them in...

    Write a C++ program that reads in 6 elements from the keyboard and stores them in an array x[6]. It should then create a second array y[6] that contains the elements from x in reverse order and with the s Tsecond - Tsmallest shown below. A sample run should look like: rocted ITrom it (e.g., Jlast first Tsmallest Vsecond last , etc). The program should finally print out both arrays to screen, in the format Enter 6 array elements: 4...

  • Part A Write a Java program that reads an integer n from the keyboard and loops...

    Part A Write a Java program that reads an integer n from the keyboard and loops until −13 ≤ n ≤ 13 is successfully entered. A do-while loop is advised. Part B Write a Java program that reads an integer n from the keyboard and prints the corresponding value n!. [This is n factorial]. You must verify that the input integer satisfies the constraint 0 ≤ n ≤ 13; keep looping until the constraint is satisfied.   Will give thumbs up...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT