Binary (base 2) uses only bits 0 and 1. Decimal (base 10) is the everyday number system. Conversion means reading each bit as a power of two and summing the contributions of every 1.
Remember
Rightmost bit = 2⁰ (value 1)
Next bit = 2¹ (value 2)
Next bit = 2² (value 4)
…
Example: 101010
1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42
Binary
Place values used
Decimal
1010
8 + 2
10
1111
8 + 4 + 2 + 1
15
101010
32 + 8 + 2
42
Approach
How to Convert Binary to Decimal in Java
Validate bits, then use a place-value loop or Integer.parseInt.
Read the binary string (only 0 and 1 allowed).
Validate every character before converting.
Sum place values (right-to-left), or call Integer.parseInt(bits, 2).
Print or return the decimal result.
Method
Idea
Time
Best for
Place-value loop
Walk right-to-left; add 2^power for each 1
O(k)
Interviews, learning place values
Integer.parseInt(s, 2)
Parse the string as base 2 in one call
O(k)
Production code, short answers
Doubling method
Left-to-right: result = result * 2 + bit
O(k)
Compact interview variant
Pseudocode
Pseudocode
function BinaryToDecimal(bits):
if bits is empty:
reject input
decimal = 0
power = 1 // 2⁰
for i from length(bits) - 1 down to 0:
ch = bits[i]
if ch is not '0' and not '1':
reject input
if ch == '1':
decimal = decimal + power
power = power * 2
return decimal
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Valid bits
Only 0 and 1
Place values
1, 2, 4, 8, 16, 32, 64, 128, …
Start side
Rightmost bit = 2⁰
Library call
Integer.parseInt(bits, 2)
Longer strings
Long.parseLong(bits, 2)
Classic check
101010 → 42
Try it
Live Preview
Enter a binary string to see place values and the decimal result.
Digits 0 and 1 only (up to 31 bits). Tap a chip or type a value.
Result
Binary = 101010
Places = 32 + 8 + 2
Decimal = 42
Trace
Worked Walkthrough — 101010
Start at the right. Multiply each bit by its place value, then add the non-zero terms.
Start with the place-value loop, then Integer.parseInt, and finish with the doubling method.
Example 1 — Place-Value Loop
Walk the string from right to left. Double the place value after each bit.
Java
class BinaryToDecimal {
static long binaryToDecimal(String bits) {
if (bits == null || bits.isEmpty()) {
return -1; // empty input
}
long decimalValue = 0;
long power = 1;
for (int i = bits.length() - 1; i >= 0; i--) {
char ch = bits.charAt(i);
if (ch != '0' && ch != '1') {
return -1; // invalid character
}
if (ch == '1') {
decimalValue += power;
}
power *= 2;
}
return decimalValue;
}
public static void main(String[] args) {
String binary = "101010";
long result = binaryToDecimal(binary);
if (result < 0) {
System.out.println("Invalid binary string.");
} else {
System.out.println(binary + " -> " + result);
}
}
}
Output
101010 -> 42
How It Works
Reject null or empty input before any math (return -1).
Start at the rightmost bit with power = 1 (that is 20).
Accept only '0' or '1'; anything else returns -1.
If the bit is '1', add the current power. A '0' adds nothing.
Double power and move one position left until the first character.
Trace — 101010
bit 0 → power 1 → skip (0)
bit 1 → power 2 → add 2
bit 0 → power 4 → skip
bit 1 → power 8 → add 8
bit 0 → power 16 → skip
bit 1 → power 32 → add 32
total = 2 + 8 + 32 = 42
Example 2 — Integer.parseInt Base 2
Validate the bits, then let the library parse the string as base 2.
Java
class BinaryParseInt {
static boolean isBinary(String bits) {
if (bits == null || bits.isEmpty()) {
return false;
}
for (int i = 0; i < bits.length(); i++) {
char ch = bits.charAt(i);
if (ch != '0' && ch != '1') {
return false;
}
}
return true;
}
static int binaryToDecimal(String bits) {
if (!isBinary(bits)) {
throw new IllegalArgumentException("Invalid binary string");
}
return Integer.parseInt(bits, 2);
}
public static void main(String[] args) {
System.out.println(binaryToDecimal("101010")); // 42
System.out.println(binaryToDecimal("1111")); // 15
}
}
Output
42
15
How It Works
isBinary rejects empty strings and any character outside 0/1.
Integer.parseInt(bits, 2) parses the string as a base-2 number.
The second argument 2 means “treat this string as binary.”
Returns a decimal int (for example "101010" → 42).
Use the manual loop in interviews; use parseInt in real code. Prefer Long.parseLong for longer strings.
Example 3 — Doubling Method
Walk left to right: multiply the running total by 2, then add the next bit.
Java
class BinaryDoubling {
static long binaryToDecimal(String bits) {
if (bits == null || bits.isEmpty()) {
return -1;
}
long result = 0;
for (int i = 0; i < bits.length(); i++) {
char ch = bits.charAt(i);
if (ch != '0' && ch != '1') {
return -1;
}
result = result * 2 + (ch - '0');
}
return result;
}
public static void main(String[] args) {
System.out.println(binaryToDecimal("101010")); // 42
}
}
Output
42
How It Works
Start with result = 0 and scan from the leftmost bit.
Each step: result = result * 2 + bit (Horner’s method).
ch - '0' turns the character '0'/'1' into the integer 0 or 1.
Same answer as place values — no separate power variable needed.
Trace for 101010: 0→1→2→5→10→21→42.
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
Reject empty input
Return an error or throw — never convert silently.
0 / 1 only
Validate every character
2, spaces, or letters make the string invalid and can throw NumberFormatException.
Leading 0
Still valid
00101 equals 5. Leading zeros do not change the value.
Overflow
Watch type limits
Prefer long / Long.parseLong for longer bit strings; use BigInteger for arbitrary length.
Analysis
Time and Space Complexity
Program
Time
Extra space
Place-value / doubling
O(k)
O(1)
Integer.parseInt(s, 2)
O(k)
O(1)
k is the number of bits in the input string.
Remember
Key Takeaways
Place values: rightmost bit is 2⁰; only 1-bits add to the total.
Validate first: reject empty strings and any character outside 0/1.
Two options: manual loop, or Integer.parseInt(s, 2).
Test set:101010 → 42; 1111 → 15; 0 → 0.
One line: sum powers of two for each 1-bit — or call Integer.parseInt(bits, 2).
Frequently Asked Questions
Sum powers of two for each 1-bit, or call Integer.parseInt(bits, 2). Both turn a base-2 string into a base-10 integer.
It parses string s as a base-2 number and returns an int. Use Long.parseLong for longer bit strings.
Binary may contain only 0 and 1. Validation avoids wrong answers and NumberFormatException.
No. Strings like 00101 are valid and equal 5 in decimal.
Both the manual loop and parseInt are O(k), where k is the number of bits. Extra space is O(1) beyond the input.