Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************
- Programmer: Tommy Lam
- Last Edit Date: 9/3/2015
- Filename: TommyLamLab1-SignedInts
- **********************************************************************/
- #include <iostream>
- using namespace std;
- void decimalToBinaryConversion(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion);
- // Function to invert all the binary integers from 0 to 1 and 1 to 0
- // then it will add 1
- void negativeDecimalToBinary(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion);
- int main()
- {
- int userInteger = 0; // Delare the integer that user will input
- int tempInteger = 0; // Declaring a temp integer to hold user's inputted integer
- int const SIZE = 16; // Declare a constant SIZE for use with array
- // Declare array to hold the converted decimal to binary
- // Most significant powers of 2 start from left to right. From 2^15 to 2^0 -- Look up table
- int powersOfTwo[SIZE] = {32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
- char binaryConversion[SIZE] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
- cout << "This app converts an integer from -32768 to 32767 " << endl << "into 16-bit 2's complement binary format." << endl << endl;
- cout << "Created Fall 2015 for CISP 310" << endl << "by Tommy Lam" << endl << endl;
- cout << "------------------------------------------------\n"; // break lines for user to see display easier
- cout << "Please input an integer in the proper range: ";
- cin >> userInteger; // Input statement to hold user's input into userInteger
- tempInteger = userInteger;
- if ((userInteger < -32768) || (userInteger > 32767))
- {
- cout << "Sorry, your integer is out of the range.";
- }
- else
- {
- if (userInteger < 0)
- {
- negativeDecimalToBinary(tempInteger, SIZE, powersOfTwo, binaryConversion);
- }
- else
- {
- decimalToBinaryConversion(tempInteger, SIZE, powersOfTwo, binaryConversion);
- }
- }
- cout << "------------------------------------------------\n"; // break lines for user to see display easier
- cout << endl;
- cout << userInteger << " decimal is" << endl; // Display user's chosen integer
- for(int n = 0; n < SIZE; ++n)
- {
- cout << binaryConversion[n]; // display what the binary conversion of that decimal integer is
- if ((n + 1) % 4 == 0)
- {
- cout << ' ';
- }
- }
- cout << endl << endl; // emptylines
- cout << "------------------------------------------------\n"; // break lines for user to see display easier
- system ("pause"); // leaves screen up
- return 0;
- }
- void decimalToBinaryConversion(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion)
- {
- for (int n = 0; n < SIZE; ++n) // Loop to go through every element in array
- {
- if (tempInteger >= powersOfTwo[n]) // If statement that will run in the for loop until n < 16
- {
- binaryConversion[n] = '1'; // Sets the char '1' if userInteger < powersOfTwo[n]
- tempInteger = tempInteger - powersOfTwo[n]; // Reduces the user integer every time a powersOfTwo is used
- }
- else
- {
- binaryConversion[n] = '0'; // else set the char to '0'
- }
- }
- return;
- }
- // Function to invert all the binary integers from 0 to 1 and 1 to 0
- // then it will add 1
- void negativeDecimalToBinary(int tempInteger, int SIZE, int* powersOfTwo, char* binaryConversion)
- {
- tempInteger = -tempInteger; // changes the negative decimal integer into a positive
- // call void decimalToBinaryConversion to convert the integer into binary
- decimalToBinaryConversion(tempInteger, SIZE, powersOfTwo, binaryConversion);
- // invert 0's to 1's and 1's to 0's
- for (int n = 0; n < 16; ++n) // Loop to go through every element in array
- {
- if (binaryConversion[n] == '0') // if char in the array is '0', then..
- {
- binaryConversion[n] = '1'; // it will be set to '1'
- }
- else
- {
- binaryConversion[n] = '0'; // else it will be set to '0'
- }
- }
- for (int m = SIZE - 1; m >= 0; --m)
- {
- if (binaryConversion[m] == '0') // if char is the same as '0'
- {
- binaryConversion[m] = '1'; // add the 1 to the 0
- break; // breaks out of for loop
- }
- else
- binaryConversion[m] = '0'; // else set char to '0' because 1 + 1 is 0 and we carry the 1 to the element before
- }
- return;
- }
- /****************************************************************************************************
- 1. What are the smallest negative decimal number and largest positive decimal
- number that can be stored in 16 bits using 2s complement format? Why?
- - 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.
- 2. In your HLL language of choice, what procedure or operator allows you to extract
- a single character from a string?
- - In C++ you can use the operator str.at or []
- 3. In your HLL language of choice, what procedure or operator lets you concatenate
- (combine) characters into a single string?
- - In C++ you can use strcat or + concatenate characters into a single string.
- 4. In your HLL language of choice, what procedure or operator lets you replace a
- character in a string with a different character?
- - In C++ you can use str.replace to replace a character in a string with a different character.
- ******************************************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement