Skip to content

Instantly share code, notes, and snippets.

@heisid
Created July 23, 2021 18:05
Show Gist options
  • Save heisid/6f3d9dc461b2a40ae792278eddec043e to your computer and use it in GitHub Desktop.
Save heisid/6f3d9dc461b2a40ae792278eddec043e to your computer and use it in GitHub Desktop.
Mengalikan angka tanpa mengalikan
/*
* 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