Created
July 23, 2021 18:05
-
-
Save heisid/6f3d9dc461b2a40ae792278eddec043e to your computer and use it in GitHub Desktop.
Mengalikan angka tanpa mengalikan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Perkalian tanpa perkalian :D | |
* | |
* sid | |
*/ | |
#include <iostream> | |
bool ganjilkah(int n) { | |
return (n & 1); | |
} | |
int kali(int x, int y) { | |
int z{0}; | |
while(x) { | |
if(ganjilkah(x)) | |
z = z + y; | |
x = x >> 1; | |
y = y << 1; | |
} | |
return z; | |
} | |
int main(int argc, char** argv) { | |
if(argc != 3) | |
return -1; | |
int x{std::stoi(argv[1])}; | |
int y{std::stoi(argv[2])}; | |
std::cout << "x = " << x << std::endl; | |
std::cout << "y = " << y << std::endl; | |
std::cout << "x * y = " << kali(x,y) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment