aboutsummaryrefslogtreecommitdiff
path: root/challenge-192/ulrich-rieke/cpp/ch-1.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-192/ulrich-rieke/cpp/ch-1.cpp')
-rw-r--r--challenge-192/ulrich-rieke/cpp/ch-1.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-192/ulrich-rieke/cpp/ch-1.cpp b/challenge-192/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..f4ca6ba3bb
--- /dev/null
+++ b/challenge-192/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,33 @@
+#include <iostream>
+#include <cstdlib>
+#include <algorithm>
+#include <vector>
+
+int main( int argc , char * argv[] ) {
+ if ( argc != 2 ) {
+ std::cerr << "usage : <programname> <positive integer>\n" ;
+ return 1 ;
+ }
+ int number = std::atoi( argv[ 1 ] ) ;
+ std::vector<int> binaryDigits ;
+ while ( number != 0 ) {
+ binaryDigits.push_back( number % 2 ) ;
+ number /= 2 ;
+ }
+ std::reverse( binaryDigits.begin( ) , binaryDigits.end( ) ) ;
+ for ( int i = 0 ; i < binaryDigits.size( ) ; i++ ) {
+ if ( binaryDigits[ i ] == 0 )
+ binaryDigits[ i ] = 1 ;
+ else
+ binaryDigits[ i ] = 0 ;
+ }
+ std::reverse( binaryDigits.begin( ) , binaryDigits.end( ) ) ;
+ int sum = 0 ;
+ int multiplier = 1 ;
+ for ( int n : binaryDigits ) {
+ sum += n * multiplier ;
+ multiplier *= 2 ;
+ }
+ std::cout << sum << std::endl ;
+ return 0 ;
+}