JohnClarenceMagdaelCIT129_LAB015

py

School

College of Southern Nevada *

*We aren’t endorsed by this school

Course

129

Subject

Computer Science

Date

Apr 3, 2024

Type

py

Pages

2

Report

Uploaded by BaronFishMaster1477

# Name: John Clarence F. Magdael # Assignment: LAB015 # Course: CIT-129 # Date of completion: 12/5/2023 # Time to complete: 1 hour # Program description: This lab aims to enhance the ability of the programmer to # convert a binary to given requirements of the lab. # Convert Binary to Decimal: 10110110 # Decimal: 182 # Convert Binary to Octal: 11011101 # Octal: 335 # Convert Binary to Hexadecimal: 00110011 # Hexadecimal: 33 # Convert binary to 2's Complement: 10010110 # 2's Complement: 01101010 # Add 2 Binary numbers: # 10101111 175 # + 00010011 19 # Answer: 11000010 194 # Subtract 2 Binary numbers: # 10011011 155 # - 00010111 23 # Answer: 10000100 132 # Subtract the part 6 using two's complement # 10011011 155 - 00010111 23 # Answer: 10000100 132 # Start of code # ------------------------------------------------------------------------------ # @brief - This function multiply the binary numbers and return the product # @param - binary_number - binary number # @param - weight - weight of the binary number # @return - bin - the value of the binary number # ------------------------------------------------------------------------------ def multiply_weight(bin, weight): for i in range(8): bin[i] = bin[i] * weight[i] return bin # ------------------------------------------------------------------------------ # @brief - This function converts the binary numbers to decimal # @param - bin - binary number # @param - weight - weight of the binary number # @return - decimal - the value of the binary number in decimal # ------------------------------------------------------------------------------ def binary_decimal(bin, weight):
decimal = 0 for i in range(8): decimal = decimal + (bin[i] * weight[i]) return decimal # Main Function def main(): bin = [] # Array for the bin input weight = [128, 64, 32, 16, 8, 4, 2, 1] # Weight as a 8-bit binary number bin_one = [1, 0, 0, 1, 1, 0, 0, 1] # bin_one given for the function # Ask the user for an 8-bit binary number and store in a list bin_input = input("Enter an 8-bit binary number: ") # Loop for each number will be assigned as an element to list bin for num in bin_input: bin.append(int(num)) print("binary list: ", bin) # Print the result # Get the bin into the weight value and output the result weighted_bin = multiply_weight(bin, weight) print("weighted bin: ", weighted_bin) # Get the binary value into decimal value and output the result decimal_value = binary_decimal(bin_one, weight) print("Decimal value: ", decimal_value) main() # Starts the function
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help