Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////REMEMBER TO PLACE signedbyte.h AND signedbyte.cpp IN SEPARATE FILES//////////
- //////////[signedbyte.h]//////////
- #ifndef SIGNED_BYTE_H
- #define SIGNED_BYTE_H
- #include <string>
- /*
- -----------------------------------
- FILE INFORMATION:
- -----------------------------------
- Class: SignedByte
- Author: Jack McKay
- Last update: 25/11/2014 13:00
- -----------------------------------
- BYTE CREATION:
- -----------------------------------
- SignedByte objectName; [Byte with value of 0 created]
- SignedByte objectName(-34); [Byte with chosen decimal value created]
- SignedByte objectName(1, 1, 0, 0, 1, 0, 1, 1); [Byte with individual bits set created]
- -----------------------------------
- ERROR CORRECTION:
- -----------------------------------
- objectName.logError(error code); [Log an error, using the error codes below]
- objectName.checkBits(); [Check that all bits are 0 or 1, if not then assigned to 1]
- -----------------------------------
- ERROR CODES:
- -----------------------------------
- 1: Tried to assign a bit as a non 0 or 1 value
- 2: Tried to create a byte with a value exceeding the maximum possible
- 3: Tried to create a byte with a value exceeding the minimum possible
- -----------------------------------
- MODIFICATION OPERATIONS:
- -----------------------------------
- objectName.setBit(bit position, bit value); [Set the bit at the specified position to the specified value]
- objectName.setByte(decimal value); [Set the byte equal to the specified decimal value]
- objectName.setByte(1, 0, 0, 1, 1, 1, 0, 1); [Reset the byte with the specified bit values]
- -----------------------------------
- BOOLEAN OPERATIONS:
- -----------------------------------
- objectName.equalsAANDB(A, B); [Set the byte equal to the result of A AND B]
- objectName.equalsAORB(A, B); [Set the byte equal to the result of A OR B]
- objectName.equalsAXORB(A, B); [Set the byte equal tothe result of A XOR B]
- objectName.equalsNOTA(A); [Set the byte equal to the result of NOT A]
- objectName.equalsAROTATEX(A, X); [Set the byte equal to the result of A rotated X to the right]
- objectName.equalsASHIFTX(A, X); [Set the byte equal to the result of A shifted X to the right]
- -----------------------------------
- ARITHMETIC OPERATIONS:
- -----------------------------------
- objectName.equalsNegativeA(A); [Set the byte equal to the result of -A]
- objectName.equalsAAddB(A, B); [Set the byte equal to the result of A + B]
- objectName.equalsASubtractB(A, B); [Set the byte equal to the result of A - B]
- -----------------------------------
- OUTPUT OPERATIONS:
- -----------------------------------
- objectName.getBit(bit position); [Returns the value of the bit at the specified position]
- objectName.getByte(); [Returns a string of the binary representation of the byte]
- objectName.getDecimal(); [Returns a signed integer of the decimal value of the byte]
- -----------------------------------
- */
- typedef unsigned int sbUINT;
- typedef signed int sbSINT;
- class SignedByte {
- public:
- static const sbSINT MAX_VALUE = 127;
- static const sbSINT MIN_VALUE = -128;
- //Constructors/Destructors
- SignedByte();
- SignedByte(sbSINT);
- SignedByte(sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT);
- ~SignedByte();
- //Bit modification operations
- void setBit(sbUINT, sbUINT);
- void setByte(sbSINT);
- void setByte(sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT);
- //Error correcting operations
- void checkBits();
- void logError(sbUINT);
- //Logical operations
- void equalsAANDB(SignedByte, SignedByte);
- void equalsAORB(SignedByte, SignedByte);
- void equalsAXORB(SignedByte, SignedByte);
- void equalsNOTA(SignedByte);
- void equalsAROTATEX(SignedByte, sbUINT);
- void equalsASHIFTX(SignedByte, sbUINT);
- //Arithmetic operations
- void equalsNegativeA(SignedByte);
- void equalsAAddB(SignedByte, SignedByte);
- void equalsASubtractB(SignedByte, SignedByte);
- //Output operations
- sbUINT getBit(sbUINT);
- std::string getByte();
- sbSINT getValue();
- private:
- sbUINT bit[8];
- };
- #endif
- //////////[signedbyte.cpp]//////////
- #include "signedbyte.h"
- #include <iostream>
- #include <string>
- SignedByte::SignedByte() {
- for (int i = 0; i < 8; i++) {
- this->bit[i] = 0;
- }
- this->checkBits();
- }
- SignedByte::SignedByte(sbSINT decValue) {
- if (decValue > MAX_VALUE) {
- this->logError(2);
- this->setByte(MAX_VALUE);
- }
- else if (decValue < MIN_VALUE) {
- this->logError(3);
- this->setByte(MIN_VALUE);
- }
- else {
- this->setByte(decValue);
- }
- this->checkBits();
- }
- SignedByte::SignedByte(sbUINT bitA, sbUINT bitB, sbUINT bitC, sbUINT bitD, sbUINT bitE, sbUINT bitF, sbUINT bitG, sbUINT bitH) {
- this->bit[0] = bitA;
- this->bit[1] = bitB;
- this->bit[2] = bitC;
- this->bit[3] = bitD;
- this->bit[4] = bitE;
- this->bit[5] = bitF;
- this->bit[6] = bitG;
- this->bit[7] = bitH;
- this->checkBits();
- }
- SignedByte::~SignedByte() {
- }
- void SignedByte::setBit(sbUINT bitPos, sbUINT bitVal) {
- this->bit[bitPos] = bitVal;
- this->checkBits();
- }
- void SignedByte::setByte(sbSINT decValue) {
- sbSINT weight[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
- for (int i = 0; i < 8; i++) {
- if ((decValue & weight[i]) == weight[i]) {
- this->bit[i] = 1;
- }
- else {
- this->bit[i] = 0;
- }
- }
- }
- void SignedByte::setByte(sbUINT bitA, sbUINT bitB, sbUINT bitC, sbUINT bitD, sbUINT bitE, sbUINT bitF, sbUINT bitG, sbUINT bitH) {
- this->bit[0] = bitA;
- this->bit[1] = bitB;
- this->bit[2] = bitC;
- this->bit[3] = bitD;
- this->bit[4] = bitE;
- this->bit[5] = bitF;
- this->bit[6] = bitG;
- this->bit[7] = bitH;
- this->checkBits();
- }
- void SignedByte::checkBits() {
- bool errorFound = false;
- for (int i = 0; i < 8; i++) {
- if ((this->bit[i] != 0) && (this->bit[i] != 1)) {
- this->bit[i] = 1;
- bool errorFound = true;
- }
- }
- if (errorFound) {
- this->logError(1);
- }
- }
- void SignedByte::logError(sbUINT errorCode) {
- std::string errorMsg = "";
- switch (errorCode) {
- case 1:
- errorMsg += "Bit set to a non 0 or 1 value!";
- break;
- case 2:
- errorMsg += "Byte cannot exceed values of ";
- errorMsg += std::to_string(MAX_VALUE);
- errorMsg += "!";
- break;
- case 3:
- errorMsg += "Byte cannot exceed values of ";
- errorMsg += std::to_string(MIN_VALUE);
- errorMsg += "!";
- break;
- default:
- errorCode = 0;
- errorMsg += "INVALID ERROR CODE!";
- break;
- }
- std::cout << "\nError #" << errorCode << ": " << errorMsg << std::endl;
- }
- void SignedByte::equalsAANDB(SignedByte A, SignedByte B) {
- for (int i = 0; i < 8; i++) {
- this->bit[i] = A.bit[i] & B.bit[i];
- }
- }
- void SignedByte::equalsAORB(SignedByte A, SignedByte B) {
- for (int i = 0; i < 8; i++) {
- this->bit[i] = A.bit[i] | B.bit[i];
- }
- }
- void SignedByte::equalsAXORB(SignedByte A, SignedByte B) {
- for (int i = 0; i < 8; i++) {
- this->bit[i] = A.bit[i] ^ B.bit[i];
- }
- }
- void SignedByte::equalsNOTA(SignedByte A) {
- for (int i = 0; i < 8; i++) {
- this->bit[i] = (A.bit[i] == 1) ? 0 : 1;
- }
- }
- void SignedByte::equalsAROTATEX(SignedByte A, sbUINT X) {
- sbUINT temp = 0;
- for (int i = 0; i < X; i++) {
- temp = A.bit[7];
- for (int j = 7; j > 0; j--) {
- A.bit[j] = A.bit[j - 1];
- }
- A.bit[0] = temp;
- }
- for (int i = 0; i < 8; i++) {
- this->bit[i] = A.bit[i];
- }
- }
- void SignedByte::equalsASHIFTX(SignedByte A, sbUINT X) {
- for (int i = 0; i < X; i++) {
- for (int j = 7; j > 0; j--) {
- A.bit[j] = A.bit[j - 1];
- }
- A.bit[0] = 0;
- }
- for (int i = 0; i < 8; i++) {
- this->bit[i] = A.bit[i];
- }
- }
- void SignedByte::equalsAAddB(SignedByte A, SignedByte B) {
- bool hasCarry = false;
- for (int i = 7; i >= 0; i--) {
- if ((A.bit[i] == 1) && (B.bit[i] == 1) && (hasCarry)) {
- this->bit[i] = 1;
- hasCarry = true;
- }
- else if ((A.bit[i] == 1) && (B.bit[i] == 1) && (!hasCarry)) {
- this->bit[i] = 0;
- hasCarry = true;
- }
- else if (((A.bit[i] == 1) || (B.bit[i] == 1)) && (hasCarry)) {
- this->bit[i] = 0;
- hasCarry = true;
- }
- else if (((A.bit[i] == 1) || (B.bit[i] == 1)) && (!hasCarry)) {
- this->bit[i] = 1;
- hasCarry = false;
- }
- else if (((A.bit[i] == 0) && (B.bit[i] == 0)) && (hasCarry)) {
- this->bit[i] = 1;
- hasCarry = false;
- }
- else if (((A.bit[i] == 0) && (B.bit[i] == 0)) && (!hasCarry)) {
- this->bit[i] = 0;
- hasCarry = false;
- }
- }
- }
- void SignedByte::equalsASubtractB(SignedByte A, SignedByte B) {
- B.equalsNegativeA(B);
- this->equalsAAddB(A, B);
- }
- void SignedByte::equalsNegativeA(SignedByte A) {
- for (int i = 0; i < 8; i++) {
- A.bit[i] = (A.bit[i] == 1) ? 0 : 1;
- }
- SignedByte one(1);
- this->equalsAAddB(A, one);
- }
- sbUINT SignedByte::getBit(sbUINT bitPos) {
- return this->bit[bitPos];
- }
- std::string SignedByte::getByte() {
- std::string byteStr = "";
- for (int i = 0; i < 8; i++) {
- if (this->bit[i] == 1) {
- byteStr += "1";
- }
- else {
- byteStr += "0";
- }
- }
- return byteStr;
- }
- sbSINT SignedByte::getValue() {
- sbSINT decValue = 0;
- sbUINT weight[8] = { -128, 64, 32, 16, 8, 4, 2, 1 };
- for (int i = 0; i < 8; i++) {
- if (this->bit[i] == 1) {
- decValue += weight[i];
- }
- }
- return decValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement