Advertisement
rplantiko

Binary Digit Sum in ABAP

Sep 12th, 2014
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 1.91 KB | None | 0 0
  1. report zz_test_binary.
  2.  
  3. parameters: x type x length 20 default '8060403A' visible length 40.
  4.  
  5. * Binary Digit Sums
  6.  
  7. * The following is an optimized algorithm to compute the binary digit sum
  8. * of an hex string (should work for arbitrary length)
  9.  
  10. * If a bit sequence is used to model a set, the binary digit sum can be
  11. * interpreted as the cardinality of the set.
  12.  
  13. * DSUM256 should be a CONSTANT, but a literal can only have 255 characters...
  14. data gc_dsum256(256) type x.
  15. perform build_dsum256 changing gc_dsum256.
  16.  
  17. data: sum   type i value 0,
  18.       count type i,
  19.       byte  type x.
  20.  
  21. write: / x.
  22.  
  23. count = xstrlen( x ).
  24. while count > 0.
  25.   subtract 1 from count.
  26.   byte = x+count(1).
  27.   sum = sum + gc_dsum256+byte(1).
  28. endwhile.
  29.  
  30. write: / '... has binary digit sum', sum.
  31.  
  32. form build_dsum256 changing cv_dsum256 type raw256.
  33.  
  34.   data: part(32) type x,
  35.         partoff type i value 0.
  36.   define _build_dsum256.
  37.     part = &1.
  38.     cv_dsum256+partoff(32) = part.
  39.     add 32 to partoff.
  40.   end-of-definition.
  41.   _build_dsum256 :
  42.     '0001010201020203010202030203030401020203020303040203030403040405',
  43.     '0102020302030304020303040304040502030304030404050304040504050506',
  44.     '0102020302030304020303040304040502030304030404050304040504050506',
  45.     '0203030403040405030404050405050603040405040505060405050605060607',
  46.     '0102020302030304020303040304040502030304030404050304040504050506',
  47.     '0203030403040405030404050405050603040405040505060405050605060607',
  48.     '0203030403040405030404050405050603040405040505060405050605060607',
  49.     '0304040504050506040505060506060704050506050606070506060706070708'.
  50.  
  51. * Digit sums of the first 256 numbers,
  52. * computed in a JS console with the following code block:
  53. *
  54. *  t=[];s="'";for (var i=0;i<256;i++) { s+= "0"+(function(i){
  55. *   s = 0; while (i > 0) { s += (i%2); i = i>>1; } return s;
  56. *  })(i); if ((i+1)%32==0){t.push(s+"'");s="'"}};t.join(",\n")+"."
  57. *
  58.  
  59. endform.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement