Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Starter code for rbg.c #include #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

image text in transcribedimage text in transcribed

Starter code for rbg.c #include  #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 

Starter code for binaryFunction.c #include  #define SIZE 32 void printBinary(int n ){ printf("binary: "); int cou = SIZE-1; int k[SIZE]={0}; while (n) { if (n & 1) k[cou]=1; //printf("1"); else k[cou]=0; //printf("0"); n >>= 1; cou--; } int i=0; for(; i  3.2 lmplementation In this exercise, you are going to use bitwise operations to pack input R,G and B values into an integer, and then use bitwise operations again to unpack the packed integer to retrieve the R, G and B values. Download and complete lab3RGB.c. This C program keeps on reading input from the stdin. Each input contains 3 integers representing the R, G and B value respectively, and then outputs the 3 values with their binary representations. The binary representations are generated by calling function void printBinary (int val), which is defined for you in another program binaryFunction.c.(How do you use a function that is defined in another file?) Next is the pack part that you should implement. This packs the 3 input values, as well as Alpha value which is assumed to be 100, into integer variable rgb pack. Then the value of rgb_pack and its binary representation is displayed (implemented for you) Next you should unpack the R, G and B value from the packed integer rgb_pack. After that, the unpacked R,G and B value and their Binary, Octal and Hex representations are displayed (implemented for you) The program terminates when you enter a negative number for either R, G or B value. Hint: Packing might be a little easier than unpacking. Considering shifting R,G,B values to the proper positions and then somehow merge them into one integer (how about bitwise OR?). For unpacking, you can either do shifting+ masking, or, masking + shifting, or, shifting only. Shifting + masking means you first shift the useful bits to the proper positions, and then turn off (set to 0) the unwanted bits while keeping the values of the useful bits. What you want to end up with, for example for R value, is a binary representation of the following, which has decimal value 2. 00 0 0 01 00 0 0 0 0 00 001 0 00 0l 0l 0l 0l0l0| 0 0l 0l00l 011 0 Masking + shifting means you first use & to turn off some unrelated bits and keep the values of the good bits, and then do a shifting to move the useful bits to the proper position. When doing shifting, the rule of thumb is to avoid right shifting on signed integers. Explore different approaches for unpacking. Finally, it is interesting to observe that function printBinary () itself uses bitwise operations to generate artificial r'1. It is recommended that, after finishing this lab, you take a look at the code ofprintBinary yourself. 3.3 Sample Inputs/Outputs red 339 % .out enter R value: 1 enter G value: 3 enter B value: 5 A: 100 binary: 00000000 00000000 00000000 01100100 binary: 00000000 00000000 00000000 00000001 binary: 00000000 00000000 00000000 00000011 binary: 00000000 00000000 00000000 00000101 B: 5 Packed: binary: 01100100 00000001 00000011 00000101 1677787909) Unpacking . R: binary: 00000000 00000000 00000000 00000001 1,01, 0xi) G: binary: 00000000 00000000 00000000 00000011 (3,03,0x3) B: binary: 00000000 00000000 00000000 00000101 (5,05, OxS) enter R value (0 255): 22 enter G value (0-255) 33 enter B value (0-255) 44 A: 100 binary: 00000000 00000000 00000000 01100100 R: 22 binary: 00000000 00000000 00000000 00010110 G: 33 binary: 00000000 00000000 00000000 00100001 B: 44 binary: 00000000 00000000 00000000 00101100 Packed: binary: 01100100 00010110 00100001 00101100 (1679171884) Unpacking R: binary: 00000000 00000000 00000000 00010110 (22, 026, 0x16) G: binary: 00000000 00000000 00000000 00100001 (33, 041, 0x21) B: binary: 00000000 00000000 00000000 00101100 44, 054, 0x2c) enter R value: 123 enter G value: 224 enter B value: 131 A: 100 binary: 00000000 00000000 00000000 01100100 R: 123 binary: 00000000 00000000 00000000 01111011 G: 224 binary: 00000000 00000000 00000000 11100000 B 13 binary: 00000000 00000000 00000000 10000011 Unpacking R: binary: 00000000 00000000 00000000 01111011 (123, 0173, 0X7B) G: binary: 00000000 00000000 00000000 11100000 (224, 0340, 0XEO) B: binary: 00000000 00000000 00000000 10000011 131, 0203, 0x83) enter R value: 254 enter G value: 123 enter B value: 19 A: 100 binary: 00000000000000000000000001100100 R: 254 binary: 00000000 00000000 00000000 11111110 G: 123 binary: 00000000 00000000 00000000 01111011 B: 19 binary: 00000000 00000000 00000000 00010011 Unpacking R: binary: 00000000 00000000 00000000 11111110 (254, 0376, 0XFE) G: binary: 00000000 00000000 00000000 01111011 (123, 0173, 0X7B) B: binary: 00000000 00000000 00000000 00010011 (19, 023, 0X13) enter R value: -3 enter G value: 3 enter B value: 56 red 340

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+What happens to nominal interest rates?

Answered: 1 week ago

Question

Does it use a maximum of two typefaces or fonts?

Answered: 1 week ago