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
34 changes: 26 additions & 8 deletions lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
# A method named `binary_to_decimal` that receives as input an array of size 8.
# The array is randomly filled with 0’s and 1’s.
# The most significant bit is at index 0.
# The least significant bit is at index 7.
# Calculate and return the decimal value for this binary number using
# the algorithm you devised in class.
def binary_to_decimal(binary_array)
raise NotImplementedError
#Write a method named binary_to_decimal that receives as input parameter an array of size 8. The array is randomly filled
# with 0’s and 1’s. Assuming that the array represents an eight bit binary number with the most significant bit at index
# 0 and the least significant bit at index 7, calculate the decimal value for this binary number using the algorithm you
# devised in class. Return the decimal value.
#
# Note: Do not use Ruby functions. You may use .length method on the array and retrieve values at a particular index in
# the array. Author using the algorithm you devised in class.
#
# You may however write your own versions of these methods

#test_array = [1, 0, 1, 1, 0, 0, 1, 1]
# the decimal number of test array should be 179

def binary_to_decimal(array)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This works, but it doesn't scale. Can you see how to make this into a loop?

bit_1 = array[0] * (2**7)
bit_2 = array[1] * (2**6)
bit_3 = array[2] * (2**5)
bit_4 = array[3] * (2**4)
bit_5 = array[4] * (2**3)
bit_6 = array[5] * (2**2)
bit_7 = array[6] * (2**1)
bit_8 = array[7] * (2**0)
result = bit_1 + bit_2 + bit_3 + bit_4 + bit_5 + bit_6 + bit_7 + bit_8
return result
end

#puts binary_to_decimal(test_array)