Bitwise operations in Algorithm Research

Source: Internet
Author: User

 

1. Use bitwise multiplication.

After a multiplier is converted to a binary system, the bitwise operation is used to multiply the multiplier.

  1. /*
  2. * Input: positive integer k and positive integer m
  3. * Output: k * m
  4. */
  5. _ Int 64 km (_ int64 k, _ int64 m ){
  6. _ Int64 x = k;
  7. Int W = (INT) floor (log (m)/log (2)-1;
  8. _ Int64 E = 1 <W;
  9. For (INT I = 0; I <= W; I ++ ){
  10. X <= 1;
  11. If (M & E)
  12. X + = K;
  13. E >>= 1;
  14. }
  15. Return X;
  16. }

/* <Br/> * input: positive integer k and positive integer m <br/> * output: k * m <br/> */<br/>__ int 64 km (_ int64 k, _ int64 m) {<br/> _ int64 x = k; <br/> int w = (int) floor (log (m)/log (2)-1; <br/> _ int64 e = 1 <w; <br/> for (int I = 0; I <= w; I ++) {<br/> x <= 1; <br/> if (m & e) <br/> x + = k; <br/> e >>= 1; <br/>}< br/> return x; <br/>}

2. Multiplication using bits

After the exponent is converted to a binary value, the bitwise operation is used to complete the multiplication.

Pseudocode:

C ++ implementation

[Cpp]
View plaincopyprint?
  1. /*
  2. * Input: positive integers v mod m and g mod m
  3. * Output: g ^ v mod m
  4. */
  5. _ Int64 gvmm (_ int64 g, _ int64 v, _ int64 m ){
  6. Int w = (int) floor (log (v)/log (2)-1;
  7. _ Int64 E = 1 <W;
  8. _ Int64 x = g;
  9. For (INT I = 0; I <= W; I ++ ){
  10. X = (x * X) % m;
  11. If (V & E ){
  12. X = (G * X) % m;
  13. }
  14. E >>= 1;
  15. }
  16. Return X;
  17. }

/* <Br/> * input: positive integer v mod m and G mod m <br/> * output: G ^ V mod m <br/> */<br/>__ int64 gvmm (_ int64 g, _ int64 V, _ int64 m) {<br/> int W = (INT) floor (log (v)/log (2)-1; <br/> _ int64 E = 1 <W; <br/> _ int64 x = g; <br/> for (INT I = 0; I <= W; I ++) {<br/> X = (x * X) % m; <br/> If (V & E) {<br/> X = (G * X) % m; <br/>}< br/> E >>= 1; <br/>}< br/> return X; <br/>}

Test of the multiplication party:

Use common algorithms and bitwise algorithms for comparison.

[Cpp]
View plaincopyprint?
  1. # Include <iostream>
  2. # Include <math. h>
  3. # Include <time. h>
  4. Using namespace STD;
  5. /*
  6. * Input: positive integers v mod m and G mod m
  7. * Output: g ^ v mod m
  8. */
  9. _ Int64 gvmm (_ int64 g, _ int64 v, _ int64 m ){
  10. Int w = (int) floor (log (v)/log (2)-1;
  11. _ Int64 e = 1 <w;
  12. _ Int64 x = g;
  13. For (int I = 0; I <= w; I ++ ){
  14. X = (x * x) % m;
  15. If (v & e ){
  16. X = (g * x) % m;
  17. }
  18. E >>= 1;
  19. }
  20. Return x;
  21. }
  22. /*
  23. * Common algorithms for verification results
  24. */
  25. Int yanzheng (_ int64 g, _ int64 v, _ int64 m ){
  26. _ Int64 x = 1;
  27. For (int I = 0; I <v; I ++ ){
  28. X * = g;
  29. X % = m;
  30. }
  31. Return x;
  32. }
  33. Int main (){
  34. Clock_t begin = clock ();
  35. Cout <yanzheng (23229,1892123, 23894) <endl;
  36. Clock_t end = clock ();
  37. Double cost = (double) (end-begin)/CLOCKS_PER_SEC;
  38. Printf ("putong: % lf seconds \ n", cost );
  39. Begin = clock ();
  40. Cout <gvmm (23229,1892123, 23894) <endl;
  41. End = clock ();
  42. Cost = (double) (end-begin)/CLOCKS_PER_SEC;
  43. Printf ("weiyunsuan: % lf seconds \ n", cost );
  44. System ("pause ");
  45. }

# Include <iostream> <br/> # include <math. h> <br/> # include <time. h> <br/> using namespace STD; </P> <p>/* <br/> * input: positive Integers v mod m and G mod m <br/> * output: G ^ V mod m <br/> */<br/>__ int64 gvmm (_ int64 G, _ int64 V, _ int64 m) {<br/> int W = (INT) floor (log (v)/log (2)-1; <br/> _ int64 E = 1 <W; <br/> _ int64 x = g; <br/> for (INT I = 0; I <= W; I ++) {<br/> X = (x * X) % m; <br/> If (V & E) {<br/> X = (G * X) % m; <br/>}< br/> E >>= 1; <br/>}< br/> return X; <br/>}< br/>/* <br/> * common algorithm of the verification result <br/> */<br/> int yanzheng (_ int64 G, _ int64 V, _ int64 m) {<br/> _ int64 x = 1; <br/> for (INT I = 0; I <V; I ++) {<br/> X * = g; <br/> X % = m; <br/>}< br/> return X; <br/>}</P> <p> int main () {<br/> clock_t begin = clock (); <br/> cout <yanzheng (23229,1892123, 23894) <Endl; <br/> clock_t end = clock (); <br/> double cost = (double) (end-begin)/clocks_per_sec; <br/> printf ("Putong: % lf seconds \ n", cost); <br/> begin = clock (); <br/> cout <gvmm (23229,1892123, 23894) <Endl; <br/> end = clock (); <br/> Cost = (double) (end-begin)/clocks_per_sec; <br/> printf ("weiyunsuan: % lf seconds \ n", cost); <br/> system ("pause"); <br/>}< br/>
According to the slightly complex multiplication operations given in the program, the efficiency is (after multiple measurements, it is almost the same order of magnitude)

21963
Putong: 0.071000 seconds
21963
Weiyunsuan: 0.001000 seconds

A common algorithm is 50-70 times slower than a bit algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.