Advertisement
Shaun_B

Bitwise operators to multiply

Sep 13th, 2012
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. /**
  6.  * A programme demonstrates using bitwise operators for multiplication
  7.  * Tested using Code::Blocks and Windows 7 Professional SP1
  8.  *
  9.  * @Author:     Shaun B
  10.  * @Version:    1.0.0.1 - 2012-09-13
  11.  * @Todo:
  12.  *
  13.  **/
  14.  
  15. static int a=0;
  16. static int times[12];
  17. int main();
  18.  
  19. int main()
  20. {
  21.     system("cls");
  22.     printf("Demonstration of using bitwise operators to multiply whole numbers.\n\n");
  23.     printf("Please enter a number\n");
  24.     printf("C:\\>");
  25.     scanf("%d",&a);
  26.     times[ 0]=  0;
  27.     times[ 1]=  a;                              /**Times 1*/
  28.     times[ 2]=  a<<1;                           /**Times 2*/
  29.     times[ 3]=  (a<<1)+a;                       /**Times 3*/
  30.     times[ 4]=  a<<2;                           /**Times 4*/
  31.     times[ 5]=  (a<<2)+a;                       /**Times 5*/
  32.     times[ 6]=  (a<<2)+(a<<1);                  /**Times 6*/
  33.     times[ 7]=  (a<<2)+(a<<1)+(a);              /**Times 7*/
  34.     times[ 8]=  a<<3;                           /**Times 8*/
  35.     times[ 9]=  (a<<3)+(a);                     /**Times 9*/
  36.     times[10]=  (a<<3)+(a<<1);                  /**Times 10*/
  37.     times[11]=  (a<<3)+(a<<1)+(a);              /**Times 11*/
  38.     times[12]=  (a<<3)+(a<<2);                  /**Times 12*/
  39.     for(int i=0; i<=12; i++)
  40.     {
  41.         printf("%4d x %4d = %10d\n", a, i, times[i]);
  42.     }
  43.     printf("\nPress the any key to end demonstration...");
  44.     _getch();
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement