(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. 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.
//Program needs to accept character input from keyboard and store in packCharacters
//Output should be the characters in their bit format before and after they are packed in to
//the unsigned int to prove they are packed correctly.
#include<stdio.h>
unsigned packCharacters(unsigned c1, char c2);
void display(unsigned val);
int main(void)
{
//Define variables
char a;
char b;
char d;
char e;
unsigned result;
unsigned result1;
unsigned result2;
//Prompt user to enter 4 characters
printf("Enter any four characters:");
//Read 4 characters
scanf("%c%c%c%c",&a, &b, &d, &e);
//display 1st char in bits
printf("Representation of '%c' in bits as an unsigned integer is:\n", a);
display(a);
//2nd char in bits
printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", b);
display(b);
//3rd char in bits
printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", d);
display(d);
//4th char in bits
printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", e);
display(e);
unsigned ch = a;
// Call function "packCharacters()" and display resutls
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);
//call the function
display(result2);
return 0;
}
// function to pack 4 characters in an unsigned integer
unsigned packCharacters(unsigned c1, char c2)
{
unsigned pack = c1;
//shift 8 bits to the left
pack <<= 8;
//using or operator pack c2
pack |= c2;
return pack;
}
void display(unsigned val)
{
//bit counter
unsigned c;
unsigned mask = 1<<31;
printf("%7u = ", val);
//loop through bits
for (c = 1; c <= 32; c++)
{
//shift 1 bit to the left
val & mask ? putchar('1') : putchar('0');
val <<= 1;
if (c % 8 == 0)
{
//print blank space
printf("");
}
}
//print new line character
putchar('\n');
}
how do I get the bits to show in sets of 8 for example : 120 = 00000000 00000000 000000000 01111000
right now mine shows as 120=00000000000000000000000001111000
Note: To print 8-bits wise. Please put a space in the print statement in the display method. Check the green color highlighted statement in the below code.
Program Screenshot:
Sample output:
Code to be copied:
/********************************************************
* Program needs to accept character input from keyboard *
* and store in packCharacters. *
* Output should be the characters in their bit format *
* Before and after they are packed into the unsigned *
* integer to prove they are packed correctly. *
********************************************************/
//Declare header files
#include
unsigned packCharacters(unsigned c1, char c2);
void display(unsigned val);
//main method
int main(void)
{
//Define variables for character type
char a;
char b;
char d;
char e;
//declare integers
unsigned result;
unsigned result1;
unsigned result2;
//Prompt user to enter 4 characters
printf("Enter any four characters:\n\n");
//Prompt and read first character
printf("Enter the %s character :", "first");
scanf("%c", &a);
getchar();
//Prompt and read second character
printf("Enter the %s character :", "second");
scanf("%c", &b);
getchar();
//Prompt and read third character
printf("Enter the %s character :", "third");
scanf("%c", &d);
getchar();
//Prompt and read fourth character
printf("Enter the %s character :", "fourth");
scanf("%c", &e);
getchar();
//Display 1st char in bits
printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", a);
//call the function to display the character in binary
display(a);
//Display the 2nd char in bits
printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", b);
display(b);
//Display the 3rd char in bits
printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", d);
display(d);
//Display the 4th char in bits
printf("\nRepresentation of '%c' in bits as an unsigned integer is:\n", e);
display(e);
unsigned ch = a;
// Call function "packCharacters()" and display resutls
result = packCharacters(ch, b);
result1 = packCharacters(result, d);
result2 = packCharacters(result1, e);
//print the
printf("\nRepresentation of '%c\''%c\''%c\' and '%c\'"
"packed in an unsigned integer is:\n", a, b, d, e);
//call the function
display(result2);
getchar();
return 0;
}
// Implement the function to pack 4 characters
//in an unsigned integer
unsigned packCharacters(unsigned c1, char c2)
{
//declare the integer pack
unsigned pack = c1;
//shift 8 bits to the left
pack
//using or operator pack c2
pack |= c2;
return pack;
}
//implement function to
//display the characters
void display(unsigned val)
{
//bit counter
unsigned c;
unsigned mask = 1
printf("%7u = ", val);
//loop through bits
for (c = 1; c
{
//shift 1 bit to the left
val & mask ? putchar('1') : putchar('0');
val
if (c % 8 == 0)
{
//print blank space
printf(" ");
}
}
//print new line character
putchar('\n');
}
(Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...
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...
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...
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...
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");...
Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of up to 100 digits (plus a sign). The calculator has a simple user interface, and 10 \variables" (n0, n1, ..., n9) into which hexadecimal integers can be stored. For example a session with your calculator might look like: mac: ./assmt1 > n0=0x2147483647 > n0+0x3 > n0? 0x214748364A > n1=0x1000000000000000000 > n1+n0 > n1? 0x100000000214748364A > n0? 0x214748364A > exit mac: Note: \mac: " is...
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...
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods Create a folder called Unit03 and put all your source files in this folder. Write a program named Unit03Prog1.java. This program will contain a main() method and a method called printChars() that has the following header: public static void printChars(char c1, char c2) The printChars() method will print out on the console all the characters between c1 and c2 inclusive. It will print 10...
1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...
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)...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...