Saturday, 10 February 2018

Python Handling Binary ,Hex and octal Numbers - Day 6(posted next day)


  • its better to have a basic idea about binary and hexadecimal nos,since the computers deal with them, here are the basics
  •   
    # #decimal numbers in binary
    # for i in range(10):
    #     print("{0:>2} in binary is {0:>8b}".format(i))
    #
    # print(0b1011)
    # #hex decimal
    # for i in range(257):
    #     print("{0:>2} in hex is {0:>02x}".format(i))
    #
    # #hex multipliation
    # x = 0x20
    # y = 0x0a
    # print(x*y)
    # #operations like or,and,xor,add,subtract refreshed
    
    #converting decimal to binary through program
    # print(10//2)
    # print(10%3)
    # powers = []
    # for power in range(15,-1,-1):
    #     powers.append(2**power)
    #     # print(powers)
    # print(powers)
    # x = int(input("enter any number less than 65535 to convert to binary \n"))
    # for i in powers:
    #     # print(i)
    #     print(x//i,end ='')
    #     x%=i
    #the above program will work ,but to avoid the trialing 0's can use the below code
    powers = []
    for power in range(15,-1,-1):
        powers.append(2**power)
        # print(powers)
    print(powers)
    printing = False
    x = int(input("enter any number less than 65535 to convert to binary \n"))
    for i in powers:
        # print(i)
        bit = x//i
        if bit!=0 or i ==1:
            printing =True
        if printing==True:
            print(bit,end ='')
        x%=i
    
    
    
     
    

    No comments:

    Post a Comment