You are not logged in.

  • Login

1

Wednesday, January 11th 2012, 10:30pm

Binär <--> Dezimal (Bitset: Nur bis 255 möglich)

Nabend,
ich habe ein Problem mit dem Umwandeln von Dezimal-Zahlen ins Dualsystem. Wenn ich Zahlen bis 255 eingebe und umwandeln lasse, tritt kein Problem auf. Wenn ich aber eine größere Zahl eingebe, fängt das Programm wieder von 0 an zu zählen.

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <bitset>
#include <string>
#include <typeinfo>
 
using namespace std;
 
 
int main ()
{
	unsigned long long int number = NULL;
	cin >> number;
 
	bitset<8> binary (number);
 
	cout << "Zahl: " << binary.to_ulong() << "(" << number << ")" << endl;
	cout << "Binaer: " << binary << endl;
 
	//Überprüfung was für Variablentyp vorliegt
	cout << "Var-Typ: " << typeid(binary.to_ulong()).name();
 
	return 0;
}


Als Fehlersuchhilfe hab ich mir mal den Variabln-Typen ausgeben lassen, der jedoch "m" ist. Ich hab noch nie von so einem Variablen-Typ gehört :D
Hab momentan keine Ansätze, wie man das Problem lösen könnte. 255 ist ja die max Anzahl des Var-Typs char. Mehr ist mir noch nicht eingefallen.

Wäre dankbar für Problemlösungen oder Lösungsansätze :D

2

Wednesday, January 11th 2012, 10:49pm

Du könntest ein Bitset nehmen, dass mehr als 8 bit hat, was den 255 entspricht ;)

einfach hier ne größere Zahl als 8 eingeben:

C/C++ Quellcode

1
bitset<8> binary (number);

3

Wednesday, January 11th 2012, 10:54pm

Verdammt, doch so einfach. Hab gehofft, dass es ein Denkfehler ist :D

Danke :thumbup:

Aber was hat es mit dem Var-Type "m" auf sich?

4

Wednesday, January 11th 2012, 10:57pm

sagt mir leider auch nichts...

5

Wednesday, January 11th 2012, 11:03pm

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <bitset>
#include <string>
#include <typeinfo>
 
using namespace std;
 
 
int main ()
{
	int a = 15;
	char b = 4;
	string c("hallo");
 
	cout << typeid(a).name() << endl;
	cout << typeid(b).name() << endl;
	cout << typeid(c).name() << endl;
}


Das gibt folgendes aus:

i
c
Ss

Also würd ich mal sagen der Anfangsbuchstabe eines Variablen-Typs. MSDN sagt, dass es keinen Variablentyp mit m als Anfangsbuchstabe gibt

Similar threads

Social bookmarks