{"id":217640,"date":"2018-09-15T22:59:46","date_gmt":"2018-09-15T22:59:46","guid":{"rendered":"http:\/\/localhost\/wordpress\/index.php\/2018\/09\/15\/gsp-215-week-2-ilab-binary-representation-of-information\/"},"modified":"2018-10-24T08:56:37","modified_gmt":"2018-10-24T08:56:37","slug":"gsp-215-week-2-ilab-binary-representation-of-information","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/gsp-215-week-2-ilab-binary-representation-of-information\/","title":{"rendered":"GSP 215 Week 2 iLab Binary Representation of Information"},"content":{"rendered":"<p>GSP 215 Week 2 iLab\t\u00a0\tWeek 2 Lab&#8221;\ufffdBinary Representation of Information\t\u00a0\tScenario\tIn this lab, we will be using cygwin and Visual Studio to write C++ code to illustrate floating point error and bitwise operations\t\u00a0\tPart A:\tThe 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.\tDigital 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.\tUse single precision floating point for all numbers. Variables need to be declared as\u00a0float, and constants should be followed by an\u00a0f\u00a0suffix, as in 3.14f.\t1. \ufffd Compute the square root of 501.0f, and store the result in a variable float\u00a0x.\t2. \ufffd Multiply\u00a0x\u00a0by itself. Ideally, this would = 2. What do you get?\t3. \ufffd Multiply\u00a0x\u00a0by itself again. Ideally, this would = 4. What do you get?\t4. \ufffd Subtract\u00a0x\u00a0from the constant 251001.0f. Ideally, this should = 0.0. What do you get?\t5. \ufffd Compute the multiplicative inverse of\u00a0x\u00a0(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?\t6. \ufffd Copy and paste your source code into the lab report below, and paste a screenshot. See an example of the screenshot below.\t\u00a0\tC++ Code:\t\u00a0\t\u00a0\t\u00a0\tScreenshot:\t\u00a0\t\u00a0\t\u00a0\tSee example below:\t\u00a0\t\u00a0\t\u00a0\tPart B:\tColor 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.\t0, 0, 0\tblack\t255, 255, 255\tbright white\t255, 0, 0\tbright red\t0, 128, 0\tmedium green\t128, 128, 0\tmedium yellow\tA digital image is a two-dimensional array of RGB values, where each RGB corresponds to an on-screen pixel.\tFor 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.\tBecause 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.\tThe representation of half red, half green, half blue is 8421504, in hexadecimal it is: 0x00808080.\tWrite two functions using only shifts and bit-wise logical operations.\u00a0One 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).\tYou will need to use shift operator.\u00a0x=y<<4\u00a0assigns\u00a0x\u00a0the result of shifting\u00a0y\u00a0to the left four bits. You will also be using bitwise &#038; (AND) and | (OR). Hint: in unpack, you will need to write code like this:\u00a0r2=(rgb>>16) &#038;0xff;\u00a0to unpack the value for red. To pack the values, you will need something like this:\u00a0rgb =\u00a0r<<16|g<<8|b;\t\u00a0\t#include\u00a0<iostream>\tusing\u00a0namespace\u00a0std;\t\u00a0\t\u00a0\t\u00a0\t\u00a0int\u00a0pack (int\u00a0r,\u00a0int\u00a0g,\u00a0int\u00a0b);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GSP 215 Week 2 iLab \u00a0 Week 2 Lab&#8221;\ufffdBinary Representation of Information \u00a0 Scenario In this lab, we will be using cygwin and Visual Studio to write C++ code to illustrate floating point error and bitwise operations \u00a0 Part A: <a href=\"https:\/\/www.benedictsol.com\/blogs\/gsp-215-week-2-ilab-binary-representation-of-information\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-217640","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=217640"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/217640\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=217640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=217640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=217640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}