gegeben Zahl binär ausgeben
-
Swordfish schrieb:
Sone schrieb:
Wers toppt kriegt nen Keks.
#include <climits> #include <bitset> #include <iostream> int main( ) { std::cout << std::bitset< sizeof( int ) * CHAR_BIT >( 42 ) << "b\n"; }
Behalt den Keks ...
Lappen, es ging um die TMP -Version.
-
Hallo Nathan,
dankeschön. Jetzt ist mir das ganze klar geworden!
Viele Grüße
-
Sone schrieb:
#include <iostream> #include <iterator> template<typename T> std::string binary(T t) { unsigned short amountBits = sizeof(T) * 8; std::string rval(amountBits, '0'); while(amountBits--) if((t >>= 1) & 1) ++rval[amountBits - 1]; return rval; } int main() { for(;;) std::cout << binary( *std::istream_iterator<int>(std::cin) ) << '\n'; }
Das Ergebnis ist dann:
1 00000000000000000000000000000000 2 00000000000000000000000000000010 3 00000000000000000000000000000010 4 00000000000000000000000000000100 5 00000000000000000000000000000100
Da müsstest Du nochmal nacharbeiten.
-
tntnet schrieb:
Sone schrieb:
#include <iostream> #include <iterator> template<typename T> std::string binary(T t) { unsigned short amountBits = sizeof(T) * 8; std::string rval(amountBits, '0'); while(amountBits--) if((t >>= 1) & 1) ++rval[amountBits - 1]; return rval; } int main() { for(;;) std::cout << binary( *std::istream_iterator<int>(std::cin) ) << '\n'; }
Das Ergebnis ist dann:
1 00000000000000000000000000000000 2 00000000000000000000000000000010 3 00000000000000000000000000000010 4 00000000000000000000000000000100 5 00000000000000000000000000000100
Da müsstest Du nochmal nacharbeiten.
Komsich. Ich bin total sicher, es ging als ich es getestet hab. Warte, ich fix das gleich.
-
Musst du nicht schön langsam ins Bett!?
#include <cctype> #include <string> #include <iostream> #include <iterator> template<typename T> std::string binary(T t) { unsigned short amountBits = sizeof(T) * CHAR_BIT; std::string rval( amountBits, '0' ); while( amountBits-- ) if( t & ( 1 << amountBits - 1 ) ) rval[ sizeof(T) * CHAR_BIT - amountBits ]++; return rval; } int main() { for(;;) std::cout << binary( *std::istream_iterator<int>(std::cin) ) << '\n'; }
-
Swordfish schrieb:
Musst du nicht schön langsam ins Bett!?
Ich werd wohl um 12 langsam das Licht ausmachen.
Fixed:
#include <iostream> #include <iterator> template<typename T> std::string binary(T t) { unsigned short amountBits = sizeof(T) * 8; std::string rval(amountBits, '0'); for(;amountBits--;t >>= 1) if(t & 1) ++rval[amountBits]; return rval; } int main() { for(;;) std::cout << binary( *std::istream_iterator<int>(std::cin) ) << '\n'; }
-
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) main.cpp 19
-
Swordfish schrieb:
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) main.cpp 19
Haha, unterstützt dein Compiler kein argument dependent name lookup? :p
Aber jetz' mal ohne Scheiß, da stimmt was nicht...
-
Sone schrieb:
Haha, unterstützt dein Compiler kein argument dependent name lookup? :p
Und wo soll er hier deiner Meinung nach was finden?
-
Sone schrieb:
Haha, unterstützt dein Compiler kein argument dependent name lookup? :p
roflmao, ymmd!!
-
SeppJ schrieb:
Sone schrieb:
Haha, unterstützt dein Compiler kein argument dependent name lookup? :p
Und wo soll er hier deiner Meinung nach was finden?
Im Namensraum
std
.N3337, Header <string> Synopsis:
namespace std { //................... template<class charT, class traits, class Allocator> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const basic_string<charT,traits,Allocator>& str);
Wenn er
std::string
findet (<iostream>
also<string>
einbindet), muss er auch den Operator finden. Das wäre meine Logik.
-
stringfwd ftw.
-
Sone schrieb:
[...] (
<iostream>
also<string>
einbindet) [...] Das wäre meine Logik.Your logic is flawed.
// edit: *filmzitateratethreadaufmachengeh'*