lab6 - Lab 6 – Binary & More Iteration ...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lab 6 – Binary & More Iteration ENGR 101 Lab 6 - Binary & More Iteration This lab will teach you about how data is represented on a computer (binary) and inform you about forloops. Background This lab will have two disjoint parts, the first deals with binary numbers and converting between them and decimal. You will also learn about 2's complement notation - this is used to represent negative numbers. Part 1: Binary The first part of the lab requires you to do basic binary operations. These include conversion between binary and decimal, addition, and subtraction. Some other basic information to think about is the total possible numbers that can be made given the number of bits it can be made of. E.g. How many possible numbers can you represent using 8-bit binary? What about two's complement? For the decimal numbers below, covert them to their 8-bit binary representation (base-10 to base-2). Decimal: 91 5 97 17 73 127 42 Binary: Now that you have had practice with converting to binary, try doing it the other way. Binary: 01010111 00001010 00000011 11101011 11111011 01011010 00101010 Decimal: Now we introduce negative numbers. In order to represent these numbers you will need to use two's complement notation. With any question about two's complement, the number of bits must be specified. In this case assume that 8-bit two's complement is used. Decimal: -53 -38 -42 99 -99 -14 42 Binary: Binary: 11010011 01001010 10010001 11111001 11111111 10000000 01111111 Decimal: Now that you understand how to represent numbers in binary, it is time to do binary addition and subtraction. The following table contains pairs of 8-bit two's complement binary numbers, you will need to add them together to obtain a result. There is also a row for you to check-off if the result overflows. To confirm your answer, you may want to convert the numbers to decimal, add them, and convert it back to binary. Binary #1: 01111111 01000100 11101001 01010000 10101100 00110110 01110110 Binary #2: 00000001 11110001 10011001 01110101 00110001 01011001 11001100 Result: The last part of learning about binary is doing subtraction. For this part you can take advantage of the "flip the bits and add one" rule to first convert the number to its negated equivalent and then add it to the second number. And extra line has been provided to you for creating the intermediate (negated plus one'd) value. Binary #1: 01010000 01000000 01011111 01010011 10000000 00010110 01110000 Binary #2: 01100101 01001101 10000000 01100101 00000001 01001001 00001100 Intermediate: Result: 1 Lab 6 – Binary & More Iteration ENGR 101 With all this complete, you now understand the basics of binary numbers! Some other questions you might find interesting to answer are to figure out what are the largest and smallest numbers you can represent using an N bit two's complement binary number. Part 2: Iteration Redux For this part of the lab you will be learning about for-loops and how to use them. Since for-loops are usually used in a different context that you have not yet learned (strings and vectors), we will be teaching you some basics for now instead. You are tasked write up three different functions. Below is lab4part2.cpp: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. #include <iostream> using namespace std; void part1(); void part2(); void part3(int a, int b); int main() { //Declare Variables: //Call Procedure "part1": //Call Procedure "part2": //Call Procedure "part3": } void part1() { //Loop: Declare, Initialize, Condition, Increment: //Output Number: } void part2() { //For Each Line: //For Each Number: //Output Numbers: } void part3(int a, int b) { //For each value of a: //For each value of b: //Output Multiplication: } Try out a few numbers for the third procedure. Also, think about what the while-loop equivalents would be. 2 ...
View Full Document

Ask a homework question - tutors are online