Question

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 their bit format before and after they are packed into the unsigned integer to prove that the characters are in fact packed correctly in the unsigned variable.

Write the program in C.

Enter two characters: A B A in bits as an unsigned integers is: 65-00000000 01000001 an unsigned integers is: Bin bits as 66-00000000 01000010 A and B packed in an unsigned integer 16706-01000001 01000010

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

The code will be

#include<stdio.h>
void bin(unsigned n)//helper function to print an unsigned number in binary.
{
unsigned i;
for (i = 1 << 31; i > 0; i = i / 2)
(n & i)? printf("1"): printf("0");
}
int main() {
unsigned a,b;
printf("Enter two characters: ");
scanf(" %c %c",&a,&b);
printf("\'%c\' in bits as an unsigned integers is: \n%u = ",a,a);
bin(a);
printf("\n\'%c\' in bits as an unsigned integers is: \n%u = ",b,b);
bin(b);
printf("\n\'%c\' and \'%c\' in bits as an unsigned integers is: \n%u = ",a,b,a<<8 | b);
bin(a<<8 | b);
  
}

The output is

Enter two characters: A B A in bits as an unsigned integers is: 65 B in bits as an unsigned integers is: 66 0000000000000

Do give a thumbs up

Add a comment
Know the answer?
Add Answer to:
The left-shift operator can be used to pack two character values into an unsigned integer variable....
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....

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

  • 4. [10 marksLet us consider a system where every integer is unsigned and uses 11 bits....

    4. [10 marksLet us consider a system where every integer is unsigned and uses 11 bits. Most programming languages provide ways to manipulate the bits of integers (signed or unsigned). For instance, if r and y are two such integers, then • & is the bitwise AND operator. r&y denotes the integer whose ith bit is the ith bit of and the ith bit of y. For instance, assuming that we have unsigned integers 145310 101101011012 and 78810 = 011000101002,...

  • 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");...

  • PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut...

    PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut the The program will ask the user to enter an unsigned also how many bits for the shift operation. Display the entered operation in both decimal and binary formats, vise shirt operators to shift the bits to the right >> or the left << user to enter an unsigned integer number, choose right shift or left shift, and orauon. Display the entered number and...

  • Application Problem: Answer the following questions at the bottom of the worksheet: You are configuring a...

    Application Problem: Answer the following questions at the bottom of the worksheet: You are configuring a microcontroller (uC) to sample a signal connected to an input pin. Part of the initial setup requires that you clear (turn off) bits #17 and #2 in a 32-bit register, while leaving all other bits unchanged. To work with specific bits, we typically use a second number, called a mask, which has the bit positions we need to alter set to 1, and all...

  • 6. Consider a C program that reads two real numbers from the keyboard followed by a character where the character can b...

    6. Consider a C program that reads two real numbers from the keyboard followed by a character where the character can be one of the operators +,-, *, 1, or % providing the addition, subtraction, multiplication, division, or remainder respectively. Then the result is displayed on the screen For example, if the user types 2.0 3.0 % then your code will display: Note: In the above example the inputs are real numbers but the remainder only performs an integer operation....

  • 10.11 (Left Shifting Integers ) Left shifting an unsigned int by 1 bit is equivalent to...

    10.11 (Left Shifting Integers ) Left shifting an unsigned int by 1 bit is equivalent to multiplying the value by 2. Write function power2 that takes two integer arguments number and pow and calculates number * 2pow. You should declare pow as an unsigned integer , but remember that the result may be a negative value . Use the shift operator to calculate the result. Print the values as integers and as bits . Assume that an integer is of...

  • 1. True/ False 1.1. AL refers to the left most Byte in register EAX 1.2. The...

    1. True/ False 1.1. AL refers to the left most Byte in register EAX 1.2. The easiest way to convert from a number in base 4 to a number in hexadecimal is through direct conversion. 1.3. Unsigned means by default positive 1.4. ASCII code uses 7 bits for each character. 1.5. The MSb (most significant bit) in the hexadecimal codes of the characters in the ASCII code is always 1. 1.6. The sections (segments) of a program in an assembly...

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

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