Page 1 of 1

Simple Python

PostPosted: Wed Sep 28, 2016 9:30 pm
by LCC
I have only just started studying computer science at A-Level and I have been set this task in python. I know it probably seems easy but I am very knew to this and am unsure of how to go about doing it please help. :)

Write a program that gets the user to type in how many bits
they are using and will then tell them what the value of the
left-most column heading will be in two's complement e.g. if
they type in 4 the program should output -8.

Thanks.

Re: Simple Python

PostPosted: Wed Sep 28, 2016 9:34 pm
by micseydel
What have you tried? If you're totally unsure, try to ask as specific a question as possible. Are you stuck with input, before doing anything with it? Does input work, but you're not sure how to compute 2's complement in Python?

Re: Simple Python

PostPosted: Thu Sep 29, 2016 7:23 am
by LCC
micseydel wrote:What have you tried? If you're totally unsure, try to ask as specific a question as possible. Are you stuck with input, before doing anything with it? Does input work, but you're not sure how to compute 2's complement in Python?



I have managed to get the input:

number = int(input("How many bits are you using?"))

I just cannot figure out how to use two's complement in my code to produce the correct output.

Re: Simple Python

PostPosted: Fri Sep 30, 2016 11:31 am
by Ofnuts
LCC wrote:
micseydel wrote:What have you tried? If you're totally unsure, try to ask as specific a question as possible. Are you stuck with input, before doing anything with it? Does input work, but you're not sure how to compute 2's complement in Python?



I have managed to get the input:

number = int(input("How many bits are you using?"))

I just cannot figure out how to use two's complement in my code to produce the correct output.

You don't need to. if you have N bits you can store 2**N values. In two's complement, one half of that is positive numbers, from 0 inclusive to (2**(N-1)-1). How can you express the bounds of the other half in a similar way, and just have a small formula that uses N to compute the lower bound?