 XXXXXXXXXXRay Mitchell Page 1 of 1 of C2A2 General Information Assignment 2 C/C++ Programming II 1 2 3 C2A2 General Information 4 5 How many bits are in a byte? 6 Contrary to what many people...

How much would this be?


 1992-2019 Ray Mitchell Page 1 of 1 of C2A2 General Information Assignment 2 C/C++ Programming II 1 2 3 C2A2 General Information 4 5 How many bits are in a byte? 6 Contrary to what many people mistakenly think, the number of bits in a byte is not necessarily 8. 7 Instead, a byte is more accurately defined in the language standards as an "addressable unit of data 8 storage large enough to hold any member of the basic character set of the execution environment". 9 Specifically, this means that the number of bits in a byte is dictated by and is equal to the number of bits 10 in type char. While on the vast majority of implementations the number of bits in such an “addressable 11 unit” is 8, there have been implementations in which this has not been true and has instead been 6 bits, 12 9 bits, or some other value. To maintain compatibility with all standards-conforming implementations 13 the macro CHAR_BIT has been defined in standard header file limits.h (climits in C++) to represent 14 the number of bits in a byte on the implementation hosting that file. The implication of this is that no 15 portable program will ever assume any particular number of bits per byte but will instead use CHAR_BIT 16 in code whenever the actual number is needed. This ensures that the code will remain valid even if 17 moved to an implementation having a different number of bits per byte. It is important to note that the 18 data type of CHAR_BIT is always int. 19 20 21 How many bits are in an arbitrary data type? 22 The sizeof operator produces a count of the number of bytes of storage required to hold an object of 23 the data type of its operand (note 2.12). Except for type char, however, not all of the bits used for the 24 storage of an object are necessarily used to represent its value. Instead, some bits may simply be 25 unused “padding” needed only to enforce memory alignment requirements. As a result, simply 26 multiplying the number of bits in a char (byte) by the number of bytes in an arbitrary data type does not 27 necessarily produce the number of bits used to represent that data type’s value. Instead, the actual 28 number of “active” bits must be determined in some other way. 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 Get a Consolidated Assignment 2 Report (optional) 47 If you would like to receive a consolidated report containing the results of the most recent version of 48 each exercise submitted for this assignment, send an empty email to the assignment checker with the 49 subject line C2A2_ID, where ID is your 9-character UCSD student ID. Inspect the report carefully since it is 50 what I will be grading. You may resubmit exercises and report requests as many times as you wish 51 before the assignment deadline. 52 53 Personalized C2A2 requirements exclusively for Sabrina Trinh (U08469078) C/C++ Programming II (Section 151115) Page 1 (10/7/2020)  1992-2019 Ray Mitchell Page 1 of 2 of C2A2E1 C2A2E1 (3 points – C Program) 1 Exclude any existing source code files that may already be in your IDE project and add two new ones, 2 naming them C2A2E1_CountBitsM.h and C2A2E1_CountIntBitsF.c. Also add instructor-supplied source 3 code file C2A2E1_main-Driver.c. Do not write a main function! main already exists in the 4 instructor-supplied file and it will use the code you write. 5 6 File C2A2E1_CountBitsM.h must contain a macro named CountBitsM. 7 CountBitsM syntax: 8 This macro has one parameter and produces a value of type int. There is no prototype (since 9 macros are never prototyped). 10 Parameters: 11 objectOrType – any expression with an object data type (24, temp, printf("Hello"), etc.), 12 or the literal name of any object data type (int, float, double, etc.) 13 Synopsis: 14 Determines the number of bits of storage used for the data type of objectOrType on any machine 15 on which it is run. This is an extremely trivial macro. 16 Return: 17 the number of bits of storage used for the data type of objectOrType 18 19 File C2A2E1_CountIntBitsF.c must contain function CountIntBitsF and no #define or #include. 20 CountIntBitsF syntax: 21 int CountIntBitsF(void); 22 Parameters: 23 none 24 Synopsis: 25 Determines the number of bits used to represent a type int value on any machine on which it is run. 26 Return: 27 the number of bits used to represent a type int value 28 29 CountBitsM and CountIntBitsF must: 30 1. not assume a char/byte contains 8 or any other specific number of bits; 31 2. not call any function; 32 3. not use any external variables; 33 4. not perform any right-shifts; 34 5. not display anything. 35 36 CountBitsM must: 37 1. not use any variables; 38 2. use a macro from header file limits.h 39 40 CountIntBitsF must: 41 1. not use any macro or anything from any header file; 42 2. not be in a header file. 43 3. not perform any multiplications or divisions; 44 45 If you get an assignment checker warning regarding instructor-supplied file C2A2E1_main-Driver.c the 46 problem is actually in your CountBitsM macro. 47 48 Questions: 49 If run on the same implementation, could the value produced by CountBitsM for type int be different 50 than the value produced by CountIntBitsF? Why or why not? The answer has nothing to do with the 51 value CHAR_BIT. Place your answers as comments in the “Title Block” of file C2A2E1_CountIntBitsF.c. 52 53 Personalized C2A2 requirements exclusively for Sabrina Trinh (U08469078) C/C++ Programming II (Section 151115) Page 2 (10/7/2020)  1992-2019 Ray Mitchell Page 2 of 2 of C2A2E1 Submitting your solution 1 Send all three source code files to the assignment checker with the subject line C2A2E1_ID, where ID is 2 your 9-character UCSD student ID. 3 See the course document titled “How to Prepare and Submit Assignments” for additional exercise 4 formatting, submission, and assignment checker requirements. 5 6 7 Hints: 8 In macro CountBitsM multiply the number of bytes in the data type of its argument by the 9 implementation-dependent number of bits in a byte. 10 11 In function CountIntBitsF start with a value of 1 in a type unsigned int variable and left-shift it one 12 bit at a time, keeping count of number of shifts, until the variable’s value becomes 0. If you use a 13 plain int (which is always signed) for this purpose you have made a portability mistake. 14 Personalized C2A2 requirements exclusively for Sabrina Trinh (U08469078) C/C++ Programming II (Section 151115) Page 3 (10/7/2020)  1992-2020 Ray Mitchell Page 1 of 2 of C2A2E2 ... LSBMSB Right Rotate ... LSBMSB Right Shift Lost0 or 1 C2A2E2 (5 points – C++ Program) 1 Exclude any existing source code files that may already be in your IDE project and add two new ones, 2 naming them C2A2E2_CountIntBitsF.cpp and C2A2E2_Rotate.cpp. Also add instructor-supplied source 3 code file C2A2E2_main-Driver.cpp. Do not write a main function! main already exists in the 4 instructor-supplied file and it will use the code you write. 5 6 File C2A2E2_CountIntBitsF.cpp must contain an exact copy of the CountIntBitsF function you wrote 7 for the previous exercise, except omit the keyword void from its parameter list. 8 9 File C2A2E2_Rotate.cpp must contain function Rotate and no #define or #include. 10 Rotate syntax: 11 unsigned Rotate(unsigned object, int count); 12 Parameters: 13 object – the value to rotate 14 count – the number of bit positions & direction to rotate: negative=>left and positive=>right 15 Synopsis: 16 Produces the value of parameter object as if it had been rotated by the number of positions and in 17 the direction specified by count. 18 Return: 19 the rotated value 20 21 The Rotate function must: 22 1. Call CountIntBitsF once and only once to determine the number of bits in parameter object. 23 2. not call any function other than CountIntBitsF. 24 3. not assume a char/byte contains 8 or any other specific number of bits. 25 4. not use loops, recursion, sizeof, macros, or anything from any header file. 26 5. not implement a special case for handling a count value of 0. 27 6. not display anything. 28 29 Here are some examples for a 32-bit type unsigned int: 30 31 32 33 34 35 36 37 38 39 Explanation 40 When a pattern is “shifted” each bit shifted off the end is simply lost. In “rotation”, however, the end bits 41 are treated as if they are connected. That is, when a pattern is right-rotated the LSB (least significant 42 bit) is placed into the MSB (most significant bit MSB) rather than being lost, as would be the case with a 43 right shift: 44 45 46 47 48 Conversely, when a pattern is left-rotated the MSB is placed into the LSB rather than being lost, as would 49 be the case with a left shift: 50 51 52 53 Function Call (right rotate) Return Value Function Call (left rotate) Return Value Rotate(0xa701, 1) 0x80005380 Rotate(0xa701, -1) 0x14e02 Rotate(0xa701, 225) 0x80005380 Rotate(0xa701, -225) 0x14e02 Rotate(0xa701, 1697) 0x80005380 Rotate(0xa701, -1697) 0x14e02 Rotate(0xdefacebd, 2) 0x77beb3af Rotate(0xdefacebd, -2) 0x7beb3af7 Rotate(0xdefacebd, 194) 0x77beb3af Rotate(0xdefacebd, -194) 0x7beb3af7 Rotate(0xdefacebd, 5378) 0x77beb3af Rotate(0xdefacebd, -5378)
Oct 12, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here