GSP 215 Week 2 iLab Week 2 Lab”�Binary Representation of Information Scenario In this lab, we will be using cygwin and Visual Studio to write C++ code to illustrate floating point error and bitwise operations Part A: The goal of this exercise is to introduce you to an important aspect of numerical computing: numerical error. Games use floating point as the primary number representation for almost everything. Coordinate data stored as (x,y,z) is used to represent vertices, which in turn are used to represent triangles, which themselves are used to represent 3D objects. Digital representation of any number must be accomplished with a fixed number of bits, typically 32. However, one third, for example, has no finite representation in fixed-point binary; that is, it would require an infinite number of bits unless one uses floating point. Computation with real numbers can quickly produce results that cannot fit into 32 bits. When this happens, numbers are rounded to the closest representable number. This introduces numerical error. Evaluating a series of expressions can result in a large error, as demonstrated in this lab. Use single precision floating point for all numbers. Variables need to be declared as float, and constants should be followed by an f suffix, as in 3.14f. 1. � Compute the square root of 501.0f, and store the result in a variable float x. 2. � Multiply x by itself. Ideally, this would = 2. What do you get? 3. � Multiply x by itself again. Ideally, this would = 4. What do you get? 4. � Subtract x from the constant 251001.0f. Ideally, this should = 0.0. What do you get? 5. � Compute the multiplicative inverse of x (meaning 1/x). In order to avoid dividing by 0.0, first test if x!= 0.0, and only compute inverse if this is true. What do you get? 6. � Copy and paste your source code into the lab report below, and paste a screenshot. See an example of the screenshot below. C++ Code: Screenshot: See example below: Part B: Color displays use blends of red, green, and blue (RGB) light to create the colors you see. The digital representation of this concept is to store each red, green, and blue component as an eight-bit unsigned number. The value 255 represents the maximum displayable brightness of a single component, the color 0 represents no intensity/light, and 128 is halfway between the two extremes. Below are some triples of RGB values and what color they represent. 0, 0, 0 black 255, 255, 255 bright white 255, 0, 0 bright red 0, 128, 0 medium green 128, 128, 0 medium yellow A digital image is a two-dimensional array of RGB values, where each RGB corresponds to an on-screen pixel. For a better understanding, open an image using the program paint (under the accessories menu). Select the color menu, then edit colors, then define custom colors, and play around in the right-hand side of the pane where you can type in RGB component numbers to see what color they represent. Because each component is only eight bits, the 24 bits required for RGB is typically stored in a single 32-bit word rather than separately. This is called a packed RGB format. This is what is used to drive all computer displays. The hexadecimal representation of orange (full red, half green, no blue) is 0x00FF0800. The representation of half red, half green, half blue is 8421504, in hexadecimal it is: 0x00808080. Write two functions using only shifts and bit-wise logical operations. One takes individual red, green, and blue components as input and returns a single 32-bit word in packed format. The second does the inverse, which is called unpacking. Test your code with some simple examples. First pack the red, green, and blue, and then unpack them to see that you get what you started with. Pay attention to the types of all input and return values to make sure that they use the least number of bits required. All of these should be unsigned numbers (there are no negative colors). You will need to use shift operator. x=y<<4 assigns x the result of shifting y to the left four bits. You will also be using bitwise & (AND) and | (OR). Hint: in unpack, you will need to write code like this: r2=(rgb>>16) &0xff; to unpack the value for red. To pack the values, you will need something like this: rgb = r<<16|g<<8|b; #include
Previous answers to this question
This is a preview of an assignment submitted on our website by a student. If you need help with this question or any assignment help, click on the order button below and get started. We guarantee authentic, quality, 100% plagiarism free work or your money back.
Get The Answer