Advertisement
Jackeh

Signed Byte Class

Nov 24th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1. //////////REMEMBER TO PLACE signedbyte.h AND signedbyte.cpp IN SEPARATE FILES//////////
  2.  
  3. //////////[signedbyte.h]//////////
  4.  
  5. #ifndef SIGNED_BYTE_H
  6. #define SIGNED_BYTE_H
  7. #include <string>
  8.  
  9. /*
  10. -----------------------------------
  11. FILE INFORMATION:
  12. -----------------------------------
  13. Class:          SignedByte
  14. Author:         Jack McKay
  15. Last update:    25/11/2014 13:00
  16. -----------------------------------
  17. BYTE CREATION:
  18. -----------------------------------
  19. SignedByte objectName; [Byte with value of 0 created]
  20. SignedByte objectName(-34); [Byte with chosen decimal value created]
  21. SignedByte objectName(1, 1, 0, 0, 1, 0, 1, 1); [Byte with individual bits set created]
  22. -----------------------------------
  23. ERROR CORRECTION:
  24. -----------------------------------
  25. objectName.logError(error code); [Log an error, using the error codes below]
  26. objectName.checkBits(); [Check that all bits are 0 or 1, if not then assigned to 1]
  27. -----------------------------------
  28. ERROR CODES:
  29. -----------------------------------
  30. 1: Tried to assign a bit as a non 0 or 1 value
  31. 2: Tried to create a byte with a value exceeding the maximum possible
  32. 3: Tried to create a byte with a value exceeding the minimum possible
  33. -----------------------------------
  34. MODIFICATION OPERATIONS:
  35. -----------------------------------
  36. objectName.setBit(bit position, bit value); [Set the bit at the specified position to the specified value]
  37. objectName.setByte(decimal value); [Set the byte equal to the specified decimal value]
  38. objectName.setByte(1, 0, 0, 1, 1, 1, 0, 1); [Reset the byte with the specified bit values]
  39. -----------------------------------
  40. BOOLEAN OPERATIONS:
  41. -----------------------------------
  42. objectName.equalsAANDB(A, B); [Set the byte equal to the result of A AND B]
  43. objectName.equalsAORB(A, B); [Set the byte equal to the result of A OR B]
  44. objectName.equalsAXORB(A, B); [Set the byte equal tothe result of A XOR B]
  45. objectName.equalsNOTA(A); [Set the byte equal to the result of NOT A]
  46. objectName.equalsAROTATEX(A, X); [Set the byte equal to the result of A rotated X to the right]
  47. objectName.equalsASHIFTX(A, X); [Set the byte equal to the result of A shifted X to the right]
  48. -----------------------------------
  49. ARITHMETIC OPERATIONS:
  50. -----------------------------------
  51. objectName.equalsNegativeA(A); [Set the byte equal to the result of -A]
  52. objectName.equalsAAddB(A, B); [Set the byte equal to the result of A + B]
  53. objectName.equalsASubtractB(A, B); [Set the byte equal to the result of A - B]
  54. -----------------------------------
  55. OUTPUT OPERATIONS:
  56. -----------------------------------
  57. objectName.getBit(bit position); [Returns the value of the bit at the specified position]
  58. objectName.getByte(); [Returns a string of the binary representation of the byte]
  59. objectName.getDecimal(); [Returns a signed integer of the decimal value of the byte]
  60. -----------------------------------
  61. */
  62.  
  63. typedef unsigned int sbUINT;
  64. typedef signed int sbSINT;
  65.  
  66. class SignedByte {
  67. public:
  68.     static const sbSINT MAX_VALUE = 127;
  69.     static const sbSINT MIN_VALUE = -128;
  70.  
  71.     //Constructors/Destructors
  72.     SignedByte();
  73.     SignedByte(sbSINT);
  74.     SignedByte(sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT);
  75.     ~SignedByte();
  76.  
  77.     //Bit modification operations
  78.     void setBit(sbUINT, sbUINT);
  79.     void setByte(sbSINT);
  80.     void setByte(sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT, sbUINT);
  81.  
  82.     //Error correcting operations
  83.     void checkBits();
  84.     void logError(sbUINT);
  85.  
  86.     //Logical operations
  87.     void equalsAANDB(SignedByte, SignedByte);
  88.     void equalsAORB(SignedByte, SignedByte);
  89.     void equalsAXORB(SignedByte, SignedByte);
  90.     void equalsNOTA(SignedByte);
  91.     void equalsAROTATEX(SignedByte, sbUINT);
  92.     void equalsASHIFTX(SignedByte, sbUINT);
  93.  
  94.     //Arithmetic operations
  95.     void equalsNegativeA(SignedByte);
  96.     void equalsAAddB(SignedByte, SignedByte);
  97.     void equalsASubtractB(SignedByte, SignedByte);
  98.  
  99.     //Output operations
  100.     sbUINT getBit(sbUINT);
  101.     std::string getByte();
  102.     sbSINT getValue();
  103. private:
  104.     sbUINT bit[8];
  105. };
  106.  
  107. #endif
  108.  
  109. //////////[signedbyte.cpp]//////////
  110.  
  111. #include "signedbyte.h"
  112. #include <iostream>
  113. #include <string>
  114.  
  115. SignedByte::SignedByte() {
  116.     for (int i = 0; i < 8; i++) {
  117.         this->bit[i] = 0;
  118.     }
  119.  
  120.     this->checkBits();
  121. }
  122.  
  123. SignedByte::SignedByte(sbSINT decValue) {
  124.     if (decValue > MAX_VALUE) {
  125.         this->logError(2);
  126.         this->setByte(MAX_VALUE);
  127.        
  128.     }
  129.     else if (decValue < MIN_VALUE) {
  130.         this->logError(3);
  131.         this->setByte(MIN_VALUE);
  132.     }
  133.     else {
  134.         this->setByte(decValue);
  135.     }
  136.  
  137.     this->checkBits();
  138. }
  139.  
  140. SignedByte::SignedByte(sbUINT bitA, sbUINT bitB, sbUINT bitC, sbUINT bitD, sbUINT bitE, sbUINT bitF, sbUINT bitG, sbUINT bitH) {
  141.     this->bit[0] = bitA;
  142.     this->bit[1] = bitB;
  143.     this->bit[2] = bitC;
  144.     this->bit[3] = bitD;
  145.     this->bit[4] = bitE;
  146.     this->bit[5] = bitF;
  147.     this->bit[6] = bitG;
  148.     this->bit[7] = bitH;
  149.  
  150.     this->checkBits();
  151. }
  152.  
  153. SignedByte::~SignedByte() {
  154.  
  155. }
  156.  
  157. void SignedByte::setBit(sbUINT bitPos, sbUINT bitVal) {
  158.     this->bit[bitPos] = bitVal;
  159.     this->checkBits();
  160. }
  161.  
  162. void SignedByte::setByte(sbSINT decValue) {
  163.     sbSINT weight[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
  164.  
  165.     for (int i = 0; i < 8; i++) {
  166.         if ((decValue & weight[i]) == weight[i]) {
  167.             this->bit[i] = 1;
  168.         }
  169.         else {
  170.             this->bit[i] = 0;
  171.         }
  172.     }
  173. }
  174.  
  175. void SignedByte::setByte(sbUINT bitA, sbUINT bitB, sbUINT bitC, sbUINT bitD, sbUINT bitE, sbUINT bitF, sbUINT bitG, sbUINT bitH) {
  176.     this->bit[0] = bitA;
  177.     this->bit[1] = bitB;
  178.     this->bit[2] = bitC;
  179.     this->bit[3] = bitD;
  180.     this->bit[4] = bitE;
  181.     this->bit[5] = bitF;
  182.     this->bit[6] = bitG;
  183.     this->bit[7] = bitH;
  184.  
  185.     this->checkBits();
  186. }
  187.  
  188. void SignedByte::checkBits() {
  189.     bool errorFound = false;
  190.     for (int i = 0; i < 8; i++) {
  191.         if ((this->bit[i] != 0) && (this->bit[i] != 1)) {
  192.             this->bit[i] = 1;
  193.             bool errorFound = true;
  194.         }
  195.     }
  196.  
  197.     if (errorFound) {
  198.         this->logError(1);
  199.     }
  200. }
  201.  
  202. void SignedByte::logError(sbUINT errorCode) {
  203.     std::string errorMsg = "";
  204.     switch (errorCode) {
  205.     case 1:
  206.         errorMsg += "Bit set to a non 0 or 1 value!";
  207.         break;
  208.     case 2:
  209.         errorMsg += "Byte cannot exceed values of ";
  210.         errorMsg += std::to_string(MAX_VALUE);
  211.         errorMsg += "!";
  212.         break;
  213.     case 3:
  214.         errorMsg += "Byte cannot exceed values of ";
  215.         errorMsg += std::to_string(MIN_VALUE);
  216.         errorMsg += "!";
  217.         break;
  218.     default:
  219.         errorCode = 0;
  220.         errorMsg += "INVALID ERROR CODE!";
  221.         break;
  222.     }
  223.  
  224.     std::cout << "\nError #" << errorCode << ": " << errorMsg << std::endl;
  225. }
  226.  
  227. void SignedByte::equalsAANDB(SignedByte A, SignedByte B) {
  228.     for (int i = 0; i < 8; i++) {
  229.         this->bit[i] = A.bit[i] & B.bit[i];
  230.     }
  231. }
  232.  
  233. void SignedByte::equalsAORB(SignedByte A, SignedByte B) {
  234.     for (int i = 0; i < 8; i++) {
  235.         this->bit[i] = A.bit[i] | B.bit[i];
  236.     }
  237. }
  238.  
  239. void SignedByte::equalsAXORB(SignedByte A, SignedByte B) {
  240.     for (int i = 0; i < 8; i++) {
  241.         this->bit[i] = A.bit[i] ^ B.bit[i];
  242.     }
  243. }
  244.  
  245. void SignedByte::equalsNOTA(SignedByte A) {
  246.     for (int i = 0; i < 8; i++) {
  247.         this->bit[i] = (A.bit[i] == 1) ? 0 : 1;
  248.     }
  249.  
  250. }
  251.  
  252. void SignedByte::equalsAROTATEX(SignedByte A, sbUINT X) {
  253.     sbUINT temp = 0;
  254.  
  255.     for (int i = 0; i < X; i++) {
  256.         temp = A.bit[7];
  257.  
  258.         for (int j = 7; j > 0; j--) {
  259.             A.bit[j] = A.bit[j - 1];
  260.         }
  261.  
  262.         A.bit[0] = temp;
  263.     }
  264.  
  265.     for (int i = 0; i < 8; i++) {
  266.         this->bit[i] = A.bit[i];
  267.     }
  268. }
  269.  
  270. void SignedByte::equalsASHIFTX(SignedByte A, sbUINT X) {
  271.     for (int i = 0; i < X; i++) {
  272.         for (int j = 7; j > 0; j--) {
  273.             A.bit[j] = A.bit[j - 1];
  274.         }
  275.  
  276.         A.bit[0] = 0;
  277.     }
  278.  
  279.     for (int i = 0; i < 8; i++) {
  280.         this->bit[i] = A.bit[i];
  281.     }
  282. }
  283.  
  284. void SignedByte::equalsAAddB(SignedByte A, SignedByte B) {
  285.     bool hasCarry = false;
  286.  
  287.     for (int i = 7; i >= 0; i--) {
  288.         if ((A.bit[i] == 1) && (B.bit[i] == 1) && (hasCarry)) {
  289.             this->bit[i] = 1;
  290.             hasCarry = true;
  291.         }
  292.         else if ((A.bit[i] == 1) && (B.bit[i] == 1) && (!hasCarry)) {
  293.             this->bit[i] = 0;
  294.             hasCarry = true;
  295.         }
  296.         else if (((A.bit[i] == 1) || (B.bit[i] == 1)) && (hasCarry)) {
  297.             this->bit[i] = 0;
  298.             hasCarry = true;
  299.         }
  300.         else if (((A.bit[i] == 1) || (B.bit[i] == 1)) && (!hasCarry)) {
  301.             this->bit[i] = 1;
  302.             hasCarry = false;
  303.         }
  304.         else if (((A.bit[i] == 0) && (B.bit[i] == 0)) && (hasCarry)) {
  305.             this->bit[i] = 1;
  306.             hasCarry = false;
  307.         }
  308.         else if (((A.bit[i] == 0) && (B.bit[i] == 0)) && (!hasCarry)) {
  309.             this->bit[i] = 0;
  310.             hasCarry = false;
  311.         }
  312.     }
  313. }
  314.  
  315. void SignedByte::equalsASubtractB(SignedByte A, SignedByte B) {
  316.     B.equalsNegativeA(B);
  317.     this->equalsAAddB(A, B);
  318. }
  319.  
  320. void SignedByte::equalsNegativeA(SignedByte A) {
  321.     for (int i = 0; i < 8; i++) {
  322.         A.bit[i] = (A.bit[i] == 1) ? 0 : 1;
  323.     }
  324.  
  325.     SignedByte one(1);
  326.     this->equalsAAddB(A, one);
  327. }
  328.  
  329. sbUINT SignedByte::getBit(sbUINT bitPos) {
  330.     return this->bit[bitPos];
  331. }
  332.  
  333. std::string SignedByte::getByte() {
  334.     std::string byteStr = "";
  335.  
  336.     for (int i = 0; i < 8; i++) {
  337.         if (this->bit[i] == 1) {
  338.             byteStr += "1";
  339.         }
  340.         else {
  341.             byteStr += "0";
  342.         }
  343.     }
  344.  
  345.     return byteStr;
  346. }
  347.  
  348. sbSINT SignedByte::getValue() {
  349.     sbSINT decValue = 0;
  350.     sbUINT weight[8] = { -128, 64, 32, 16, 8, 4, 2, 1 };
  351.  
  352.     for (int i = 0; i < 8; i++) {
  353.         if (this->bit[i] == 1) {
  354.             decValue += weight[i];
  355.         }
  356.     }
  357.  
  358.     return decValue;
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement