BCD.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Binary Code Decimal resources.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2017
  6. * @license Apache-2.0
  7. */
  8. /**
  9. * BCD encoding schemes.
  10. */
  11. export const ENCODING_SCHEME = [
  12. "8 4 2 1",
  13. "7 4 2 1",
  14. "4 2 2 1",
  15. "2 4 2 1",
  16. "8 4 -2 -1",
  17. "Excess-3",
  18. "IBM 8 4 2 1",
  19. ];
  20. /**
  21. * Lookup table for the binary value of each digit representation.
  22. *
  23. * I wrote a very nice algorithm to generate 8 4 2 1 encoding programatically,
  24. * but unfortunately it's much easier (if less elegant) to use lookup tables
  25. * when supporting multiple encoding schemes.
  26. *
  27. * "Practicality beats purity" - PEP 20
  28. *
  29. * In some schemes it is possible to represent the same value in multiple ways.
  30. * For instance, in 4 2 2 1 encoding, 0100 and 0010 both represent 2. Support
  31. * has not yet been added for this.
  32. */
  33. export const ENCODING_LOOKUP = {
  34. "8 4 2 1": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  35. "7 4 2 1": [0, 1, 2, 3, 4, 5, 6, 8, 9, 10],
  36. "4 2 2 1": [0, 1, 4, 5, 8, 9, 12, 13, 14, 15],
  37. "2 4 2 1": [0, 1, 2, 3, 4, 11, 12, 13, 14, 15],
  38. "8 4 -2 -1": [0, 7, 6, 5, 4, 11, 10, 9, 8, 15],
  39. "Excess-3": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
  40. "IBM 8 4 2 1": [10, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  41. };
  42. /**
  43. * BCD formats.
  44. */
  45. export const FORMAT = ["Nibbles", "Bytes", "Raw"];