Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. // Implement a function that takes in an integer and prints out its two's complement value by following the algorithm described above. Hint: to invert the bits of a number you can use the "~" operator. For example ~25 will invert the bits of the integer 25.
  2.  
  3. function twosComplement(int){
  4. console.log('int to string is ' + int.toString(2));
  5. const binaryInt = int.toString(2);
  6. // console.log(binaryInt.length)
  7. let result = '';
  8. for (var i = 0; i < binaryInt.length; i++){
  9. if (binaryInt[i] === '1') {
  10. result = result.concat('0');
  11. } else {
  12. result = result.concat('1')
  13. }
  14. }
  15. console.log('result is ' + result)
  16. console.log(result.slice(-2))
  17. const endDigits = result.slice(-2)
  18. if (endDigits === '00') {
  19. return result.slice(0, result.length-2) + '01'
  20. } else if (endDigits === '01') {
  21. return result.slice(0, result.length-2) + '10'
  22. } else {
  23. return result.slice(0, result.length-2) + '11'
  24. }
  25. }
  26.  
  27. twosComplement(514)
  28.  
  29. --------------------------------------------------
  30. // Write a function that takes an integer value and checks to see if it is even or odd using the big-wise AND operator. Hint: Think about what the value of the least-significant bit will be for even and odd numbers.
  31.  
  32. function evenOrOdd (num) {
  33. const binNum = num.toString(2);
  34. if (binNum.slice(-1) === '1'){
  35. return 'odd';
  36. }
  37. return 'even'
  38. }
  39.  
  40. evenOrOdd(13379239774)
  41.  
  42.  
  43. --------------------------------------------------
  44. Why would using bit-wise operations be potentially faster for checking whether a number is even or odds as opposed to using something like the modulo operator (for example randInt % 2)?
  45.  
  46. ans: because there is no equation to be performed. you just need to know if the last number in binary is 1 or 0
  47.  
  48.  
  49. --------------------------------------------------
  50. Write a function that takes in two integer values and prints out the resultant value when you AND the two input values and then also when you OR the two input values.
  51.  
  52. !!this is not correct. it needs to check each position in the digital numbers against each other and return correct
  53.  
  54. function andOr (intA, intB){
  55. const lastDigitA = intA.toString(2).slice(-1);
  56. const lastDigitB = intB.toString(2).slice(-1);
  57. let resultStr = '';
  58. // AND
  59. if (lastDigitA === '1' && lastDigitB === '1'){
  60. resultStr += 'AND: 1, ';
  61. } else {
  62. resultStr += 'AND: 0, ';
  63. }
  64. // OR
  65. if(lastDigitA === '0' && lastDigitB === '0') {
  66. resultStr += 'OR: 0';
  67. } else {
  68. resultStr += 'OR: 1';
  69. }
  70. return resultStr;
  71. }
  72.  
  73. console.log(andOr(4, 6))
  74. console.log(andOr(4, 5))
  75. console.log(andOr(3, 4))
  76. console.log(andOr(3, 5))
  77.  
  78. --------------------------------------------------
  79. // Extend the previous function further by adding logic for the XOR operation when two integer values are input. Add a third parameter which denotes which type of operation to execute. Print out the resultant value for the associated operation type.
  80.  
  81. function logicalOperation (intA, intB, type) {
  82. const lastDigitA = intA.toString(2).slice(-1);
  83. const lastDigitB = intB.toString(2).slice(-1);
  84. if (type === 'AND'){
  85. if (lastDigitA === '1' && lastDigitB === '1'){
  86. return 'AND: 1';
  87. } else {
  88. return 'AND: 0';
  89. }
  90. }
  91. else if (type === 'OR'){
  92. if(lastDigitA === '0' && lastDigitB === '0') {
  93. return 'OR: 0';
  94. } else {
  95. return 'OR: 1';
  96. }
  97. }
  98. else if (type === 'XOR') {
  99. if(lastDigitA !== lastDigitB){
  100. return 'XOR: 1';
  101. } else {
  102. return 'XOR: 0';
  103. }
  104. }
  105. }
  106.  
  107. --------------------------------------------------
  108. Write a function that takes in an integer value and prints out its complement value.
  109.  
  110. function complement(int){
  111. console.log(~int)
  112. }
  113.  
  114. complement(3) // => -4
  115.  
  116. What do you notice about the numbers which are output? What about if you give a large input value?
  117.  
  118. ans: for positive integers the output is the integer made negative minus 1.
  119. for negative integers the output is the integer made positive minus 1.
  120. formula => ~x = -(x + 1)
  121.  
  122.  
  123. --------------------------------------------------
  124. // Write a function which sets the third bit of a number.
  125.  
  126. function thirdBit (int){
  127. const intBinary = int.toString(2)
  128. console.log('int in binary = ' + intBinary);
  129. console.log('4 in binary = ' + (4).toString(2))
  130. //four has a 1 in the third bit so performing an OR with the input will ensure the third bit is set
  131. const intOr4 = (int | 4);
  132. console.log('int or 4 = ' + intOr4.toString(2));
  133. return intOr4
  134. }
  135.  
  136. --------------------------------------------------
  137. // Write a function which toggles the third bit of a number.
  138.  
  139. function thirdBitToggle(int){
  140. const intBinary = int.toString(2);
  141. console.log('int in binary = ' + intBinary);
  142. console.log('4 in binary = ' + (4).toString(2))
  143. const intXor4 = (int ^ 4);
  144. console.log('int Xor 4 = ' + intXor4.toString(2));
  145. return (int ^ 4)
  146. }
  147.  
  148. thirdBitToggle(5)
  149.  
  150. --------------------------------------------------
  151. Write a function which clears (sets to zero) the third bit of a number.
  152.  
  153. function thirdBitClear(int){
  154. return (int & (~4))
  155. }
  156.  
  157. ex:
  158. 5 = 101
  159. ~4 = 011
  160. ----------
  161. & = 001 (or 1)
  162.  
  163. --------------------------------------------------
  164. Write a function which tells you whether the third bit of a number is set.
  165.  
  166. function isSet(int){
  167. if ((int & 4) !== 0) {
  168. // bit is set
  169. return '3rd bit set'
  170. } else {
  171. // bit is not set
  172. return '3rd bit not set'
  173. }
  174. }
  175.  
  176. isSet(8) // => 3rd bit not set
  177. isSet(12) // => 3rd bit set
  178.  
  179. ex
  180. 8 = 1000
  181. 4 = 0100
  182. ----------
  183. & = 0000
  184.  
  185. ex
  186. 12 = 1100
  187. 4 = 0100
  188. ----------
  189. & = 0100
  190.  
  191.  
  192. --------------------------------------------------
  193. //SHIFT OPERATORS
  194.  
  195. // it started to get complicated then i realized there were bitwise operators built into JS :P
  196.  
  197. function shifter (intA, intB){
  198. // left shift
  199. const resultLeft = `Left Shift: ${(intA * (2**(intB)))}, `;
  200. // right shift
  201. let resultRight = `Right Shift: ${intA >> intB}, `;
  202. let prefix = '';
  203. let zeroFillPrefix = '';
  204. // if(intA > 0){
  205. // for (var i = 0; i < intB; i++){
  206. // prefix += '0';
  207. // zeroFillPrefix += '0';
  208. // }
  209. // }
  210. // else if (intA < 0){
  211. // for (var j = 0; j < intB; j++){
  212. // prefix += '1';
  213. // zeroFillPrefix += '0';
  214. // }
  215. // }
  216. // const shiftRightEnd = (intA.toString(2).slice(0, -(intB)));
  217. // const shiftedRight = prefix + shiftRightEnd;
  218. // resultRight += `${parseInt(shiftedRight, 10)}, `;
  219.  
  220. // zero-fill right shift
  221. let resultZeroFill = `Zero Fill Right Shift: ${intA >>> intB}`;
  222. // const zeroShiftedRight = zeroFillPrefix + shiftRightEnd;
  223. // console.log(-17 >> 5)
  224. // resultZeroFill += `${parseInt(zeroShiftedRight, 10)}`
  225.  
  226. return resultLeft + resultRight + resultZeroFill
  227. }
  228.  
  229.  
  230. shifter(13, 3)
  231.  
  232. --------------------------------------------------
  233. // Using Shift Operators
  234. // Write a function which doubles an integer.
  235.  
  236. function doubler(int) {
  237. return int << 1
  238. }
  239.  
  240. doubler(200)
  241.  
  242.  
  243. --------------------------------------------------
  244. Write a function which quadruples an integer.
  245.  
  246. function quadrupler(int) {
  247. return int << 2
  248. }
  249.  
  250. quadrupler(2)
  251.  
  252. --------------------------------------------------
  253. // Write a function which divides an integer by two, rounding down.
  254.  
  255. function divideBy2(int) {
  256. return int >> 1
  257. }
  258.  
  259. divideBy2(567)
  260.  
  261. --------------------------------------------------
  262. // Write a function which calculates 2^n.
  263.  
  264. function twoN (n){
  265. return (1 << n)
  266. }
  267.  
  268.  
  269. twoN(5)
Add Comment
Please, Sign In to add comment