xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
int main(void){
bitset<8> bits("1000");
int ab = bits.to_ulong();
cout << ab << "\n";
return 0;
}
xxxxxxxxxx
#include <iostream>
using namespace std;
int fromBineryToDecimal(int binary);
int main(){
int decimalNum;
//convert from binary to decimal
decimalNum = fromBineryToDecimal(1010); // 1010 = 10
cout << decimalNum << endl;
}
int fromBineryToDecimal(int binary) {
int rem = 0;
int weight = 1;
int decimalNumber=0;
while (binary != 0) {
/*
*
*( any number % 10 ) = firist digit
* 123 % 10 = 3
* 1452 % 10 = 2
* 1101101 % 10 = 1
*
*/
rem = binary % 10;
binary /= 10;
// decimal = first digit*1 + second*2 + third*4 + forth*8 + ...
decimalNumber += rem * weight;
weight *= 2; // weight = 1 , 2 , 4 , 8 , 16 , 32 , ...
}
return decimalNumber;
}