Question

Please complete the following problem in C. No source code is provided. The left-shift operator can...

Please complete the following problem in C. No source code is provided.

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 int variable, 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. Repeat this process for the third and fourth characters. The program should output the characters in their bit format before and after they’re packed into the unsigned int to prove that the characters are in fact packed correctly in the unsigned int variable.

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

Packed Characters in an Integer:

  • First ofall, the Program needs to accept character input from keyboard and store in packCharacters.
  • And, the Output should be the characters in their bit format before and after they are packed in to the unsigned integer to prove they are packed correctly.    

/*Source Code*/

#include<stdio.h>

unsigned packCharacters(unsigned c1, char c2);

void display(unsigned val);

int main(void)

{

      char a;

      char b;

      char d;

      char e;

      unsigned result;

      unsigned result1;

      unsigned result2;

      printf("Enter any four characters:\n\n");

      printf("Enter the %s character :", "first");

      scanf("%c", &a);

      getchar();

      printf("Enter the %s character :", "second");

      scanf("%c", &b);

      getchar();

      printf("Enter the %s character :", "third");

      scanf("%c", &d);

      getchar();

      printf("Enter the %s character :", "fourth");

      scanf("%c", &e);

      getchar();

      printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", a);

      display(a);

      printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", b);

      display(b);

      printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", d);

      display(d);

      printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", e);

      display(e);

      unsigned ch = a;

      result = packCharacters(ch, b);

      result1 = packCharacters(result, d);

      result2 = packCharacters(result1, e);

      printf("\nRepresentation of '%c\''%c\''%c\' and '%c\'"

            "packed in an unsigned integer is:\n", a, b, d, e);

      display(result2);

      getchar();

      return 0;

}

unsigned packCharacters(unsigned c1, char c2)

{

      unsigned pack = c1;

      pack <<= 8;

      pack |= c2;

      return pack;

}

void display(unsigned val)

{

      unsigned c;

      unsigned mask = 1 << 31;

      printf("%7u = ", val);

      for (c = 1; c <= 32; c++)

      {

            val & mask ? putchar('1') : putchar('0');

            val <<= 1;

            if (c % 8 == 0)

            {

                  printf(" ");

            }

      }

      putchar('\n');

}

Sample Design of Output:

Add a comment
Know the answer?
Add Answer to:
Please complete the following problem in C. No source code is provided. The left-shift operator can...
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
  • The left-shift operator can be used to pack two character values into an unsigned integer variable....

    The left-shift operator can be used to pack two character values into an unsigned integer variable. Write a program that inputs two characters from the keyboard and passes them to function packCharacters. To pack two characters into an unsigned integer variable, assign the first character to the unsigned variable, shift the unsigned variable left by 8- bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator. The program should output the characters in...

  • (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....

  • Please help me with the following C Programming project. I am providing the code I used...

    Please help me with the following C Programming project. I am providing the code I used which needs to be reworked to add the following requirements. Here is my code #include<stdio.h> char input; int main() { int i = 0; while (true){ scanf_s("%c", &input); if (input == 'X') break; printf("%c", input); } return 0; } Here are the requirements for the code plus and additional note what the code should have. Goals Understand the ASCII representation of character values. Understand...

  • lab3RGB.c file: #include <stdio.h> #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack...

    lab3RGB.c file: #include <stdio.h> #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack; int r_unpack, g_unpack,b_unpack; int alpha = AlphaValue; printf("enter R value (0~255): "); scanf("%d",&r); printf("enter G value (0~255): "); scanf("%d",&g); printf("enter B value (0~255): "); scanf("%d",&b); while(! (r<0 || g<0 || b <0) ) { printf("A: %d\t", alpha); printBinary(alpha); printf("\n"); printf("R: %d\t", r); printBinary(r); printf("\n"); printf("G: %d\t", g); printBinary(g); printf("\n"); printf("B: %d\t", b); printBinary(b); printf("\n"); /* do the packing */ //printf("\nPacked: value %d\t", rgb_pack); printBinary(rgb_pack);printf("\n");...

  • please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj...

    please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj Input file: shadow.in in reality, when we read an English word we normally do not read every single letter of that word but rather the word's "shadow" recalls its pronunciation and meaning from our brain. The word's shadow consists of the same number of letters that compose the actual word with first and last letters (of the word) in their original positions while the...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

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