Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DIM BITS AS INTEGER ' Define total amount of bits in the byte
- BITS = 16 ' Define amount of bits to allocate to binary
- DIM BYTE1(BITS) AS INTEGER ' BITS-allocated byte
- DIM DECIM AS LONG ' Standard output byte as decimal (after translation)
- RETRY: ' Retry in case user inputs incorrect number
- PRINT "Please enter a valid number between 0 and" + STR$((2 ^ BITS) - 1); ' First prompt w/ evaluation of highest possible input number
- INPUT "> ", DECIM ' Ask for user input with second prompt and pass data into DECIM LONG (dimmed previously)
- IF DECIM > (2 ^ BITS) - 1 THEN ' Incase user inputs number higher than highest possible input number
- COLOR 12 ' Display message in red for error
- PRINT "Number cannot be over" + STR$((2 ^ BITS) - 1) + "!" ' Display number overflow error
- COLOR 7 ' Return to standard color
- GOTO RETRY ' Go back to retry prompting
- ELSEIF DECIM < 0 THEN 'Incase user inputs a negative number
- COLOR 12 ' Display message in red for error
- PRINT "Number cannot be under 0!" ' Display number underflow error
- COLOR 7 ' Return to standard color
- GOTO RETRY ' Go back to retry prompting
- END IF ' End input check if statement
- FOR X = 1 TO BITS ' Declare all bits in byte as empty
- BYTE1(X) = 0 ' Empty bit
- NEXT X ' End for loop
- ' --- MAIN ALGORITHM CODE EXECUTION HERE ---
- ' Since binary is calculated from
- ' the highest value to the lowest
- ' we need to create a for loop that
- ' counts from the highest bit to the lowest
- ' This allows the program to loop until all
- ' bits have been processed and to make sure that
- ' the toggled bits are positioned correctly
- X = BITS ' Set X to BITS to start loop at TOP
- FOR Y = 1 TO BITS ' Begin for loop to declare all bits in byte
- X = X - 1 ' Decrease X by one to down the binary chain
- MODULO = 2 ^ (X) ' Convert bit position into a base-10 decimal number
- IF MODULO <= DECIM THEN ' Is the current decimal number able to be subtracted by the converted bit position?
- BYTE1(X) = 1 ' If so, set that bit position to 1
- DECIM = DECIM - MODULO ' Take away converted bit position from the decimal number
- ELSE ' If not
- BYTE1(X) = 0 ' Set that bit position to 0
- END IF ' End if statement so as to continue the loop
- PRINT ((BYTE1(X))); ' Print that result on screen
- NEXT Y ' End bit declaration for loop
- PRINT ' Create blankline
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement