Python Forum
binary to decimal program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
binary to decimal program
#1
Hello everyone :)
I am new in this forum and also new to coding and I have been given the task to make a program that can convert binary numbers to decimal numbers. I am a math student and I have trouble with the syntax, I am not sure which syntax to use. I can easily convert from binary to decimal by hand. I have been given the following python document which has the full description of the exercise.

So my guess is I need to make a loop which can take a list and give me a integer. So in this case the list must be the binary number eg. [1, 0, 1, 0] which is the binary number 1010 and correspond to the decimal number 10. But the problem is I dont now how to write the loop-syntax correctly.


import auto_test_tools as att


'''

Write a function that takes as input a list which is used as
a mask of N elements with values 0 or 1. The function should
then compute the integer output defined by

     x = \sum_{i=1}^N  2^{i-1} mask(i)

Use a loop for computing the output value.

Test your procedure on each of the input examples below

  mask := <0 0 0 0>;
  mask := <0 0 0 1>;
  mask := <0 0 1 0>;
  mask := <0 0 1 1>;
  mask := <0 1 0 0>;
  mask := <0 1 0 1>;
  mask := <0 1 1 0>;
  mask := <0 1 1 1>;
  mask := <1 0 0 0>;

The notation <...> is a bit-pattern with least significant
bit at the left-most position and most significant bit
at the right-most position.

ADVICE: Submit your solution even if your code is
not running or your score is less than 100%

'''


def make_number(mask: list) -> int:
    """
    Generate a number from a given bit mask.

    :param mask:    The bit mask gives as a list with least significant bit first and most significant bit last.

    :return:        The decimal number corresponding ot the binary bit mask.
    """
    return -1   # TODO Write your own code here



att.start()
att.begin_task('Task 1')

mask_0 = [0, 0, 0, 0]
mask_1 = [1, 0, 0, 0]
mask_2 = [0, 1, 0, 0]
mask_3 = [1, 1, 0, 0]
mask_4 = [0, 0, 1, 0]
mask_5 = [1, 0, 1, 0]
mask_6 = [0, 1, 1, 0]
mask_7 = [1, 1, 1, 0]
mask_8 = [0, 0, 0, 1]
mask_9 = [1, 0, 0, 1]
mask_10 = [0, 1, 0, 1]
mask_11 = [1, 1, 0, 1]
mask_12 = [0, 0, 1, 1]
mask_13 = [1, 0, 1, 1]
mask_14 = [0, 1, 1, 1]
mask_15 = [1, 1, 1, 1]

tst_0 = make_number(mask_0)
tst_1 = make_number(mask_1)
tst_2 = make_number(mask_2)
tst_3 = make_number(mask_3)
tst_4 = make_number(mask_4)
tst_5 = make_number(mask_5)
tst_6 = make_number(mask_6)
tst_7 = make_number(mask_7)
tst_8 = make_number(mask_8)
tst_9 = make_number(mask_9)
tst_10 = make_number(mask_10)
tst_11 = make_number(mask_11)
tst_12 = make_number(mask_12)
tst_13 = make_number(mask_13)
tst_14 = make_number(mask_14)
tst_15 = make_number(mask_15)

att.is_equal(tst_0, 0, att.get_linenumber(), ' mask 0 failed')
att.is_equal(tst_1, 1, att.get_linenumber(), ' mask 1 failed')
att.is_equal(tst_2, 2, att.get_linenumber(), ' mask 2 failed')
att.is_equal(tst_3, 3, att.get_linenumber(), ' mask 3 failed')
att.is_equal(tst_4, 4, att.get_linenumber(), ' mask 4 failed')
att.is_equal(tst_5, 5, att.get_linenumber(), ' mask 5 failed')
att.is_equal(tst_6, 6, att.get_linenumber(), ' mask 6 failed')
att.is_equal(tst_7, 7, att.get_linenumber(), ' mask 7 failed')
att.is_equal(tst_8, 8, att.get_linenumber(), ' mask 8 failed')
att.is_equal(tst_9, 9, att.get_linenumber(), ' mask 9 failed')
att.is_equal(tst_10, 10, att.get_linenumber(), ' mask 10 failed')
att.is_equal(tst_11, 11, att.get_linenumber(), ' mask 11 failed')
att.is_equal(tst_12, 12, att.get_linenumber(), ' mask 12 failed')
att.is_equal(tst_13, 13, att.get_linenumber(), ' mask 13 failed')
att.is_equal(tst_14, 14, att.get_linenumber(), ' mask 14 failed')
att.is_equal(tst_15, 15, att.get_linenumber(), ' mask 15 failed')

att.end_task()

att.stop()
Reply
#2
We just wrote two tutorials on loops: an overview and one that goes into more details.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
You can directly use int function to convert a binary string to decimal.
x = [1, 0, 1, 0]
binStr = "".join(str(i) for i in x) # convert list to str
print(int(binStr, 2)) # convert binary string to decimal
// 10
Loop version.
print(sum(v*2**i for i, v in enumerate(reversed(x))))
// 10
Reply
#4
Yeah, fits.
2**0 == 1
2**1 == 2
2**2 == 4
...

At the end it's just the sum of it.
mask = [0, 0, 1, 1]
mask[0] * 2**0 + mask[1] * 2**1 + mask[2] * 2**2 + mask[3] * 2**3
# 0 * 2**0 + 0 * 2**1 + 1 * 2**2 + 1 * 2**3
If the first value of the mask is the most significant bit, then it's 2**3 at this position.
In this case you can use reversed, to reverse the list.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Binary to decimal, print the decimal rs74 3 2,058 Jul-12-2020, 05:25 PM
Last Post: DPaul
  converting decimal to binary missus_brown 1 2,138 Feb-19-2019, 02:42 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020