Shifting - Microsoft Azure



Comp 21000Spring 2021Problem Set 6Revision R0Due: Thursday, 18 MarchObjectivesObjectives.Understand binary, hexadecimal, and octal basesPractice conversion between basesUnderstand how to translate two’s complementPractice extracting bits from integers in CShiftingConvert each of the following decimal numbers to two’s complement binary, show the effect of a one-bit ASL operation on it, and then convert the result back to decimal. Then show the result of applying a one-bit ASR (arithmetic shift right) operation on the result from the ASL operation and its resulting binary, hex and decimal value. Finally show the result of applying a one-bit LSR (logical shift right) one bit on the result from the ASL operation and it’s resulting binary, hex and decimal value. Assume that you have 8 bits. Use the table below for your results.FirstNumberSecondNumberThirdNumberFourthNumberFifthNumber1. Number as decimal-10165-1271021272 Number as 2’s complement8-bit binary3. Number shiftedleft 1 bit as binary4. Number on row 3as hex5. Number on row 3as decimal6. Number on row 3 shiftedarithmetic right 1 bit7. Number on row 6as hex8. Number on row 6as decimal9. Number on row 3 shiftedlogical right 1 bit10. Number on row 9as hex11. Number on row 9as decimalCommandBitwise OperatorMaskProblem 1(completed as an example)x = 0x45;——————-——————-y = x&0x0F;printf(”%x”, y);(prints 0x5)——————-Problem 2(complete symbol and mask columns)x = 0x65;——————-——————-y = xprintf(”%x”, y);(prints 0x60)——————-Problem 3(complete symbol and mask columns)x = 0xA65;——————-——————-y = xprintf(”%x”, y);(prints 0xA6F)——————-Problem 4(complete symbol and mask columns)The mask must be the same in boassignment statementsx1 = 0xC95;——————-——————-x2 = 0x368;——————-——————-y = x1z = x2printf(”%x %x”, y, z);(prints 0xedd and 0xbf9 )——————-MaskingComplete the entries in the table below.thBase and ShiftingAssume that we are working with numbers in radix (or base) 14. Then each position of a number would represent a power of 14. If a number written in base 14 is shifted right one position with the sign bit shifted into the most significant digit (i.e., an ASR in base 14),What is the relationship of the new shifted number with the original number?Show that your assertion is true by using the values of the digits in each position (as we did in class). Assume that you have as many digits as necessary. Note that a base 14 number has the numbers 0-9 and the letters A-D for example if we do a shift one position right: 008D3AB1 → 0008D3ABBig & Little endiansConsider the program that we reviewed in class (it’s in the Chapter 4 Part 7 slides, slides 14 and 15; we discussed it in class). This program printed out an integer and then printed out the actual bytes stored in memory for that integer. You can find the program on the server at/home/barr/Student/comp210/resources/ps6/onsider the network problem with endian-ness. A server might store an integer in a big-endian manner, but the computer that receives the integer might store it in a little-endian manner. So a network program must be able to receive an integer and then reverse the integer’s endian-ness. We’ll do this inversion by modifying the program in the slides to do the following tasks:You must use only pointer syntax in your solution!Read in a number from the user.Print out the bytes in the number in the order in which they’re stored (on the server, this is little-endian format). For example, if the user enters 15213, this translates to 0x00003B6D. You will print the bytes in little-endian order, i.e., 0x6D, 0x3B, 0x00, 0x00. You can use the show bytes program from the slides or server to print these bytes.Change the stored bytes so that they are stored in a new integer in Big-endian manner.Print out the resulting integerPrint out the bytes of the new integer one at a time from left to right.Example. In the slides we stored the number 15213.This translates to 0x00003B6D. When we printed out the bytes, they were printed in little-endian order, i.e., 0x6D, 0x3B, 0x00, 0x00.Your program must declare a new integer variable in the function using a pointer and dynamic memory. You must extract the bytes from the original integer one at a time and store them into the bytes of the new (dynamically allocated) integer. You must store them in the opposite order of the original integer. In this example, you would store into the bytes of the new integer first 0x00, then 0x00, then 0x3B, then 0x6D.When you print out this integer (as an integer) you would get the number1,832,583,168 (i.e., the number 0x6D3B0000) in our example.Finally, after you have printed out the new integer, create a loop that traverses the bytes of the new integer and print out the bytes. In our example, you would print0x000x000x3B0x6DHint: One way to do this is to create two character pointers. One will point at the beginning of the new integer and one will point at the beginning of the original integer. You can then use array syntax to access the bytes of each integer. For the original integer, you will access the bytes from the last byte (byte 3) to the first byte (byte 0). For the new integer you will put in bytes from the first byte (byte 0) to the last byte (byte 3).Your solution should add about 11 lines of code to the original program.You do not need to include file headers or comments for your program.Turn In?Write your answers for questions 2, 3, and 4 on a separate piece of paper or on the Word version of this problem set. Take a picture of your answers, turn the picture into a pdf and submit to Sakai or, if you put your answers in the Word version of the problem set, submit your answers in Word to Sakai.?Save your ”show bytes” program in a file titled YourLastName.c, change its permissions to 600 and cp it to the directory/home/barr/Student/comp210/Turnin/ps6 on the server. Note that you can write to this directory but cannot read from it so you will not be able to see the file once you’ve written it to the directory.RevisionModificationDateR0posted problem set10 Mar 2021 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download