#Variable declaration
n = 25
#Calculations
b = bin(n)
#Result
print "The binary equivalent of 25 is",b
def binary_decimal(ni): # Function to convert binary to decimal
deci = 0;
i = 0;
while (ni != 0):
rem = ni-int(ni/10.)*10
ni = int(ni/10.);
deci = deci + rem*2**i;
i = i + 1;
return deci
def binfrac_decifrac(nf): # Function to convert binary fraction to decimal fraction
decf = 0;
i = -1;
while (i >= -3):
nf = nf*10;
rem = round(nf);
nf = nf-rem;
decf = decf + rem*2**i;
i = i - 1;
return decf
n = 101.101; # Initialize the binary number
n_int = int(n); # Extract the integral part
n_frac = n-n_int; # Extract the fractional part
#Result
print "Decimal equivalent of %7.3f = %5.3f"%(n, binary_decimal(n_int)+binfrac_decifrac(n_frac))
def octal_decimal(n): # Function to convert binary to decimal
dec = 0;
i = 0;
while (n != 0):
rem = n-int(n/10)*10;
n = int(n/10);
dec = dec + rem*8**i;
i = i + 1;
return dec
n = 173; # Initialize the octal number
#Result
print "Decimal equivalent of %d = %d"%(n, octal_decimal(n));
#Variable declaration
n = 278
#Calculations
o = oct(n)
#Result
print "The octal equivalent of 278 is",o
#Variable declaration
n1 = '10001100'
n2 = '1011010111'
#Calculations
x1 = hex(int(n1,2))
x2 = hex(int(n2,2))
#Results
print "The hexadecimal equivalent of 10001100 is",x1
print "The hexadecimal equivalent of 1011010111 is",x2
#Variable declaration
n = 72905
#Calculations
h = hex(n)
#Result
print "The hexadecimal equivalent of 278 is",h
#Variable declaration
x1 = 0b11101
x2 = 0b10111
#Calculations
x = bin(x1+x2)
#Result
print "The required result is",x
def decimal_binary(ni): # Function to convert decimal to binary
bini = 0;
i = 1;
while (ni != 0):
rem = ni-int(ni/2)*2;
ni = int(ni/2);
bini = bini + rem*i;
i = i * 10;
return bini
def decifrac_binfrac(nf): # Function to convert binary fraction to decimal fraction
binf = 0; i = 0.1;
while (nf != 0):
nf = nf*2;
rem = int(nf);
nf = nf-rem;
binf = binf + rem*i;
i = i/10;
return binf
def binary_decimal(ni): # Function to convert binary to decimal
deci = 0;
i = 0;
while (ni != 0):
rem = ni-int(ni/10)*10;
ni = int(ni/10);
deci = deci + rem*2.**i;
i = i + 1;
return deci
def binfrac_decifrac(nf): # Function to convert binary fraction to decimal fraction
decf = 0;
i = -1;
while (i >= -3):
nf = nf*10;
rem = round(nf);
nf = nf-rem;
decf = decf + rem*2.**i;
i = i - 1;
return decf
bin1 = 1011.11; # Initialize the first binary binber
bin2 = 1011.01; # Initialize the second binary binber
bin1_int = int(bin1); # Extract the integral part for first
bin1_frac = bin1-bin1_int; # Extract the fractional part for second
bin2_int = int(bin2); # Extract the integral part for first
bin2_frac = bin2-bin2_int; # Extract the fractional part for second
dec1 = binary_decimal(bin1_int)+binfrac_decifrac(bin1_frac);
dec2 = binary_decimal(bin2_int)+binfrac_decifrac(bin2_frac);
dec = dec1+dec2;
dec_int = int(dec);
dec_frac = dec-dec_int;
#Result
print "%7.2f + %7.2f = %8.2f"%(bin1, bin2, decimal_binary(dec_int)+decifrac_binfrac(dec_frac))
#Variable declaration
x1 = 0b0111
x2 = 0b1001
#Calculations
x = bin(x1-x2)
#Result
print "The required result is",x
#Variable declaration
x1 = 0b1101
x2 = 0b1100
#Calculations
x = bin(x1*x2)
#Result
print "The required result is",x
#Incorrect solution in textbook
#Variable declaration
x1 = 0b11001
x2 = 0b101
#Calculations
x = bin(x1/x2)
#Result
print "The required result is",x