def binary_to_decimal(string):
    """Converts a binary string, sign bit then msb on left, to an integer.
    Negative numbers are assumed to be twos-complements, i.e., bitwise
    complement + 1."""
    length = len(string)
    # negative part doesn't work
    if string[0] == '1':
        negative = True
    else:
        negative = False
    magnitude = 0
    for i in range(length-1,0,-1):
        # print 'index=',i
        if string[i] == '1':
            magnitude = magnitude + pow(2,length-i-1)
        # print i,string[i],magnitude
    if negative:
        magnitude = -pow(2,length-1)+magnitude
    return int(magnitude)