Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. /**********************************************************************
  2. Programmer: Tommy Lam
  3. Last Edit Date: 9/3/2015
  4. Filename: TommyLamLab1-SignedInts
  5.  
  6. **********************************************************************/
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. void decimalToBinaryConversion(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion);
  12.  
  13. // Function to invert all the binary integers from 0 to 1 and 1 to 0
  14. // then it will add 1
  15. void negativeDecimalToBinary(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion);
  16.  
  17. int main()
  18. {
  19. int userInteger = 0; // Delare the integer that user will input
  20. int tempInteger = 0; // Declaring a temp integer to hold user's inputted integer
  21. int const SIZE = 16; // Declare a constant SIZE for use with array
  22. // Declare array to hold the converted decimal to binary
  23. // Most significant powers of 2 start from left to right. From 2^15 to 2^0 -- Look up table
  24. int powersOfTwo[SIZE] = {32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
  25. char binaryConversion[SIZE] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
  26.  
  27. cout << "This app converts an integer from -32768 to 32767 " << endl << "into 16-bit 2's complement binary format." << endl << endl;
  28. cout << "Created Fall 2015 for CISP 310" << endl << "by Tommy Lam" << endl << endl;
  29. cout << "------------------------------------------------\n"; // break lines for user to see display easier
  30.  
  31. cout << "Please input an integer in the proper range: ";
  32. cin >> userInteger; // Input statement to hold user's input into userInteger
  33.  
  34. tempInteger = userInteger;
  35.  
  36. if ((userInteger < -32768) || (userInteger > 32767))
  37. {
  38. cout << "Sorry, your integer is out of the range.";
  39. }
  40. else
  41. {
  42. if (userInteger < 0)
  43. {
  44. negativeDecimalToBinary(tempInteger, SIZE, powersOfTwo, binaryConversion);
  45. }
  46. else
  47. {
  48. decimalToBinaryConversion(tempInteger, SIZE, powersOfTwo, binaryConversion);
  49. }
  50. }
  51.  
  52. cout << "------------------------------------------------\n"; // break lines for user to see display easier
  53. cout << endl;
  54.  
  55. cout << userInteger << " decimal is" << endl; // Display user's chosen integer
  56.  
  57. for(int n = 0; n < SIZE; ++n)
  58. {
  59. cout << binaryConversion[n]; // display what the binary conversion of that decimal integer is
  60. if ((n + 1) % 4 == 0)
  61. {
  62. cout << ' ';
  63. }
  64. }
  65.  
  66. cout << endl << endl; // emptylines
  67. cout << "------------------------------------------------\n"; // break lines for user to see display easier
  68.  
  69. system ("pause"); // leaves screen up
  70. return 0;
  71. }
  72.  
  73.  
  74. void decimalToBinaryConversion(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion)
  75. {
  76. for (int n = 0; n < SIZE; ++n) // Loop to go through every element in array
  77. {
  78. if (tempInteger >= powersOfTwo[n]) // If statement that will run in the for loop until n < 16
  79. {
  80. binaryConversion[n] = '1'; // Sets the char '1' if userInteger < powersOfTwo[n]
  81. tempInteger = tempInteger - powersOfTwo[n]; // Reduces the user integer every time a powersOfTwo is used
  82. }
  83. else
  84. {
  85. binaryConversion[n] = '0'; // else set the char to '0'
  86. }
  87. }
  88. return;
  89. }
  90.  
  91. // Function to invert all the binary integers from 0 to 1 and 1 to 0
  92. // then it will add 1
  93. void negativeDecimalToBinary(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion)
  94. {
  95. tempInteger = -tempInteger; // changes the negative decimal integer into a positive
  96.  
  97. // call void decimalToBinaryConversion to convert the integer into binary
  98. decimalToBinaryConversion(tempInteger, SIZE, powersOfTwo, binaryConversion);
  99.  
  100. // invert 0's to 1's and 1's to 0's
  101. for (int n = 0; n < 16; ++n) // Loop to go through every element in array
  102. {
  103. if (binaryConversion[n] == '0') // if char in the array is '0', then..
  104. {
  105. binaryConversion[n] = '1'; // it will be set to '1'
  106. }
  107. else
  108. {
  109. binaryConversion[n] = '0'; // else it will be set to '0'
  110. }
  111. }
  112. for (int m = SIZE - 1; m >= 0; --m)
  113. {
  114. if (binaryConversion[m] == '0') // if char is the same as '0'
  115. {
  116. binaryConversion[m] = '1'; // add the 1 to the 0
  117. break; // breaks out of for loop
  118. }
  119. else
  120. binaryConversion[m] = '0'; // else set char to '0' because 1 + 1 is 0 and we carry the 1 to the element before
  121. }
  122.  
  123. return;
  124. }
  125.  
  126. /****************************************************************************************************
  127.  
  128.  
  129. 1. What are the smallest negative decimal number and largest positive decimal
  130. number that can be stored in 16 bits using 2s complement format? Why?
  131. - The range of values are from -32768 through 32767. This is because a 16-bit twos complement integer can only have 2^16 unique values, including zero.
  132.  
  133. 2. In your HLL language of choice, what procedure or operator allows you to extract
  134. a single character from a string?
  135. - In C++ you can use the operator str.at or []
  136.  
  137. 3. In your HLL language of choice, what procedure or operator lets you concatenate
  138. (combine) characters into a single string?
  139. - In C++ you can use strcat or + concatenate characters into a single string.
  140.  
  141. 4. In your HLL language of choice, what procedure or operator lets you replace a
  142. character in a string with a different character?
  143. - In C++ you can use str.replace to replace a character in a string with a different character.
  144.  
  145. ******************************************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement