Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
# the algorithm you devised in class.
def binary_to_decimal(binary_array)
raise NotImplementedError
decimal = 0
8.times do |i|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job with the do ... loop. Can you think of a way to generalize the algorithm so that it works for n bits?

decimal += binary_array[i] * 2 ** (7 - i)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job with using the math operator **. Can you think of a faster way to perform this without having to use the ** operation? Your code currently requires recalculating 2 to the power of n (number of bits) at every iteration.

end
return decimal
end