fork download
  1. // ---------------------------------------------------------------------------------------------------------------------
  2. // Gabriel Malone / Week 3 / Lab 3 / CS 210
  3. // ---------------------------------------------------------------------------------------------------------------------
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. // function prototypes // ----------------------------------------------------------------------------------------------
  8. unsigned short reverse_bytes(unsigned short);
  9. unsigned short booths_algorithm(uint8_t, uint8_t);
  10. unsigned int reverse_words(unsigned int);
  11. unsigned int ascii_to_integer(char*, int size);
  12. void to_upper(char*, int size);
  13. void to_lower(char*, int size);
  14. unsigned short full_adder(uint8_t, uint8_t);
  15. char* to_bits(unsigned int);
  16. void print_bits(char*);
  17. void print_string(char*, int);
  18.  
  19. // ---------------------------------------------------------------------------------------------------------------------
  20.  
  21. // MAIN
  22.  
  23. // ---------------------------------------------------------------------------------------------------------------------
  24. int main(int argc, char** argv){
  25. // ---------------------------------------------------------------------------------------------------------------------
  26. // CONVERTING ASCII TO INTEGER DEMO
  27. // -----------------------------------------------------------------------------------------------------------------
  28. char ascii_number [] = "123456"; // c style string as pointer to char (array)
  29. printf("%d", ascii_to_integer(ascii_number, 6));
  30. // -----------------------------------------------------------------------------------------------------------------
  31. // CONVERTING LOWER TO UPPERCASE DEMO
  32. // -----------------------------------------------------------------------------------------------------------------
  33. char strA [] = "abcdefg";
  34. to_upper(strA, 7);
  35. print_string(strA, 7);
  36. // -----------------------------------------------------------------------------------------------------------------
  37. // CONVERTING UPPER TO LOWER DEMO
  38. // -----------------------------------------------------------------------------------------------------------------
  39. char strB [] = "GOOBERT";
  40. to_lower(strB, 7);
  41. print_string(strB, 7);
  42. // -----------------------------------------------------------------------------------------------------------------
  43. // REVERSING WORDS DEMO
  44. // -----------------------------------------------------------------------------------------------------------------
  45. printf("\ndword reversed: %X\n",reverse_words(0xABCDEF00));
  46. // -----------------------------------------------------------------------------------------------------------------
  47. // REVERSING BYTES DEMO
  48. // -----------------------------------------------------------------------------------------------------------------
  49. printf("short word reversed: %X\n",reverse_bytes(0xABCD));
  50. // -----------------------------------------------------------------------------------------------------------------
  51. // PRINTING BITS DEMO
  52. // -----------------------------------------------------------------------------------------------------------------
  53. print_bits(to_bits(19)); // enter any number up to 32 bits
  54. // -----------------------------------------------------------------------------------------------------------------
  55. // FULL ADDER DEMO
  56. // -----------------------------------------------------------------------------------------------------------------
  57. printf("\n%d",full_adder(11,255));
  58. // -----------------------------------------------------------------------------------------------------------------
  59. // BOOTHS DEMO
  60. // -----------------------------------------------------------------------------------------------------------------
  61. printf("\n%d", booths_algorithm(100,12));
  62.  
  63. }
  64. // ---------------------------------------------------------------------------------------------------------------------
  65.  
  66. // METHODS
  67.  
  68. // ---------------------------------------------------------------------------------------------------------------------
  69.  
  70. // ---------------------------------------------------------------------------------------------------------------------
  71. unsigned int reverse_words(unsigned int dword){
  72. // -----------------------------------------------------------------------------------------------------------------
  73. return ( (dword & 0x0000FFFF) << 16) + ( (dword & 0xFFFF0000) >> 16); // dword & LO mask << to HO pos and vice-versa
  74. }
  75.  
  76. // ---------------------------------------------------------------------------------------------------------------------
  77. unsigned short reverse_bytes(unsigned short word){
  78. // -----------------------------------------------------------------------------------------------------------------
  79. return ( (word & 0x00FF) << 8) + ( (word & 0xFF00) >> 8); // word & LO mask << to HO pos and vice-versa
  80. }
  81.  
  82. // ---------------------------------------------------------------------------------------------------------------------
  83. unsigned short booths_algorithm(uint8_t mcnd, uint8_t mplr){
  84. // -----------------------------------------------------------------------------------------------------------------
  85. unsigned short itr = 0, PB = 0, CB = mplr & 1, LO = mplr, HO=0,product = 0x0000, phb;
  86. while (itr ++ < 8) // need to loop for 8 bits
  87. {
  88. if (CB ^ PB) // if xor results in true
  89. { // add multiplicand to the HO byte
  90. if (CB) HO = full_adder(HO, full_adder(~mcnd, 1)); // twos compliment addition for subtraction
  91. if (PB) HO = full_adder(HO, mcnd); // addition
  92. }
  93. product = (HO << 8) | LO; // new product is HO byte shifted into 16 bit position, w multiplier in LO position
  94. PB = product & 1; // store previous end bit
  95. phb = product & 0x8000; // check if highest bit is 0 or 1 before shifting to maintain
  96. product >>= 1; // shift product to the right one
  97. if (phb) product |= 0x8000; // force HO to 1 if originally was 1
  98. CB = product & 1; // get current end bit
  99. HO = product >> 8; // set new HO byte (the HO byte of the product)
  100. LO = product & 0x00FF; // set new LO byte
  101. }
  102. return product;
  103. }
  104.  
  105. // ---------------------------------------------------------------------------------------------------------------------
  106. unsigned int ascii_to_integer(char *ascii_number, int size){
  107. //------------------------------------------------------------------------------------------------------------------
  108. unsigned int converted = 0x00000000;
  109. for (int i = 0 ; i < size ; i ++ ) { // clear HO 11's then convert each number to its correct decimal position
  110. converted = converted * 10 + ( *ascii_number ++ &= ~( 3 << 4 ) );
  111. } // 0 + 1 = 1 * 10 = 10, 10 + 2 = 12, 12*10 = 120 + 3 = 123, etc.
  112. return converted; // then set to 0 to clear those bits with &. the result is multiplied by current base 10 position
  113. }
  114.  
  115. // ---------------------------------------------------------------------------------------------------------------------
  116. void to_upper(char* d, int size){
  117. // -----------------------------------------------------------------------------------------------------------------
  118. for (int i = 0 ; i < size ; i ++) *d ++ &= ~ ( 1 << 5 );
  119. // set 1, move it into position, flip it to zero, and the bit with that zero to clear
  120. }
  121. // ---------------------------------------------------------------------------------------------------------------------
  122.  
  123. // ---------------------------------------------------------------------------------------------------------------------
  124. void to_lower(char* c, int size){
  125. // -----------------------------------------------------------------------------------------------------------------
  126. for (int i = 0 ; i < size ; i ++) *c ++ |= 1 << 5; // set 1 , move to position , or the 1 with the bit to set to 1
  127. }
  128.  
  129. // ---------------------------------------------------------------------------------------------------------------------
  130. unsigned short full_adder(uint8_t one, uint8_t two) {
  131. // -----------------------------------------------------------------------------------------------------------------
  132. // ~~ deep seek helped fix my original attempt which failed to carry correctly sometimes & did not have final carry
  133. // -----------------------------------------------------------------------------------------------------------------
  134. int shift = 0;
  135. uint8_t carry = 0;
  136. uint16_t sum = 0; // Store 8-bit sum + carry-out in bit 8
  137. // -----------------------------------------------------------------------------------------------------------------
  138. while (shift < 8) { // go through all 8 bits
  139. uint8_t a_bit = (one >> shift) & 1; // start from LO bits
  140. uint8_t b_bit = (two >> shift) & 1; // start from LO bits
  141. uint8_t sum_bit = a_bit ^ b_bit ^ carry; // sum_bit is 1 when there's an odd number of 1s in the inputs
  142. carry = (a_bit & b_bit) | (a_bit & carry) | (b_bit & carry); // carry-out exists if at least 2 of 3 inputs are 1
  143. sum |= (sum_bit << shift ++ ); // set the current bit with the sum_bit then increment shift for next loop
  144. }
  145. return sum |= (carry << 8); // when done, store any carry-out in the 9th bit (bit 8)
  146. }
  147.  
  148. // ---------------------------------------------------------------------------------------------------------------------
  149. char *to_bits(unsigned int num){
  150. // -----------------------------------------------------------------------------------------------------------------
  151. char* bits = malloc(sizeof(unsigned int) * sizeof(char));// 4 bytes for int and 1 byte for char = 4 bytes or 32 bits
  152. int shifts = 0; // how many shifts will be performed (32, 1 per bit)
  153. // -----------------------------------------------------------------------------------------------------------------
  154. while (shifts < 32) // start shifting
  155. *bits ++ = ( num & 1 << shifts ++ ) ? '1' : '0'; // deref the char array, inc post, mask w/ << 1, pick outcome
  156. // -----------------------------------------------------------------------------------------------------------------
  157. return bits; // return the pointer (will need to be re-wound)
  158. }
  159.  
  160.  
  161. // ---------------------------------------------------------------------------------------------------------------------
  162. // extra helpers
  163. // ---------------------------------------------------------------------------------------------------------------------
  164. void print_bits(char* ptr){
  165. // -----------------------------------------------------------------------------------------------------------------
  166. int cntr = 0; // while pointer isn't null, backtrack from current pointer address in memory, print char at mem add
  167. while (*--ptr) (cntr == 4) ? cntr = 0 , printf("_%c",*ptr) : printf("%c",*ptr) , cntr ++ ;
  168. }
  169. // ---------------------------------------------------------------------------------------------------------------------
  170.  
  171.  
  172. // ---------------------------------------------------------------------------------------------------------------------
  173. void print_string(char* string, int size){
  174. // -----------------------------------------------------------------------------------------------------------------
  175. for (int i = 0 ; i < size ; i ++) (i == 0) ? printf("\n%c", string[i]) : printf("%c", string[i]);
  176. }
  177. // ---------------------------------------------------------------------------------------------------------------------
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
123456
ABCDEFG
goobert
dword reversed: EF00ABCD
short word reversed: CDAB
0000_0000_0000_0000_0000_0000_0001_0011
266
1200