how do you convert binary to integer?

for example 101010
http://www.cplusplus.com/reference/stl/bitset/bitset/

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <bitset>

int main ()
{
  std::string s("101010");
  std::bitset<6> num(s); // 6 "bits" in the string
  std::cout << "String \"" << num.to_string() << "\" == " << num.to_ulong() << std::endl;
  return 0;
}

im looking for the algorithm like is it 2^(n-1)
for example 1010 have n=4 2^3=8 but 1010=10
so how do you get it
Last edited on
A quick tut:

Take the decimal integer:

104

This can be mathematically expressed as:

104 = 1 * 10^2 + 0 * 10^1 + 4 * 10^0

Since decimal is a base ten system, the bases for the exponential operation is 10.

Now, take the binary:

100101

This can be expressed in decimal as:

100101b = 1 * 2^5 + 0 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 =
32 + 0 + 0 + 4 + 0 + 1 =
37
Last edited on
Topic archived. No new replies allowed.