Statistics: Posted by Seb McClouth — Thu Feb 07, 2008 5:48 pm
Code:
'Inputs: YourNum% - The number to turn into binary.'Outputs: Bin$ - The number in binary, formatted as a string.DO MaxBit% = MaxBit% + 1LOOP UNTIL 2 ^ MaxBit% > YourNum%'Remove the part above and replace the MaxBit% below if you already'know the highest bit your program will have, or if you can only take'certain bits (ie, up to 16)FOR A = 0 TO MaxBit% Bin$ = LTRIM$(RTRIM$(STR$(ABS(((2 ^ A) AND YourNum%) > 0)))) + Bin$NEXT A
Statistics: Posted by Patz QuickBASIC Creations — Thu Feb 07, 2008 5:43 pm
Statistics: Posted by Seb McClouth — Thu Feb 07, 2008 5:26 pm
Code:
SUB Dec2BinCOLOR 14: LOCATE 2, 30: PRINT "Decimal to Binary"COLOR 10: LOCATE 5, 20: INPUT "Enter a decimal number: ", num&DOremain = num& MOD 2 'remainder is used for binary resultnum& = num& \ 2 'discard any remainder with integer divisionBin$ = LTRIM$(STR$(remain)) 'make string numberBinary$ = Bin$ + Binary$ 'add remainder to binary numberLOOP UNTIL num& = 0COLOR 11: LOCATE 10, 30: PRINT "Binary number = "; Binary$ 'binary resultEND SUB
Statistics: Posted by burger2227 — Thu Feb 07, 2008 2:54 pm
Code:
FOR a = 0 TO 20 'I'm assuming for the test we only need 20 bits If temp1 AND 2^a THEN 'as Ted told me to test, I'm testing here if it goes temp$ = "1"+ temp$ 'Why did I assume again this has to be one? temp2 = temp2 + (temp1 AND 2 ^ a) 'to test if temp2 = temp1 then IF temp2 = temp1 THEN EXIT FOR 'we leave the for-next-thing ELSE temp$ = "0" + temp$ END IF NEXT
simple exits the for-next if the calculated bits are the same as the input. So if we have 500 for example we have 9 bits, 200 for example 8 bits, if I'm not mistaken.Statistics: Posted by Seb McClouth — Wed Feb 06, 2008 12:23 pm
Code:
YourValue% XOR 16
should give you what you're looking for. That's saying if you pull out the 4 bits you needed into the YourValue%, that is.Statistics: Posted by Patz QuickBASIC Creations — Tue Feb 05, 2008 11:56 pm
Code:
temp1 = 500 'we gonna complementair 500 in this example.temp$ = ""temp2 = 0mirrortemp$ = ""temp3 = 0compltemp$ = ""mirrorcompltemp$ =""temp4 = 0FOR a = 0 TO 20 'I'm assuming for the test we only need 20 bits If temp1 AND 2^a THEN 'as Ted told me to test, I'm testing here if it goes temp$ = "1"+ temp$ 'Why did I assume again this has to be one? temp2 = temp2 + (temp1 AND 2 ^ a) 'to test if temp2 = temp1 then IF temp2 = temp1 THEN EXIT FOr 'we leave the for-next-thingELSE temp$ = "0" + temp$END IFNEXT'The above code gives us the bits for temp1FOR a = 1 to len (temp$)mirrortemp$ = MID$(temp$, a, 1) + mirrortemp$ 'mirror the bit from temp$ for checkNEXT'from here I'm lost in remarks, but it works.FOR a = 1 TO LEN(mirrortemp$)IF MID$(mirrortemp$, a, 1) = "1" THEN IF a = 1 THEN temp3 = temp3 + 1 ELSE temp3 = temp3 + 2 ^ (a-1) END IFEND IFNEXTIF temp3 = temp1 THEN 'compare temp3 and temp1FOR a = 1 TO LEN(temp$) IF MID$(temp$, a, 1) = "0" THEN compltemp$ = compltemp$ + "1" ELSEIF MID$(temp$, a, 1) = "1" THEN compltemp$ = compltemp$ + "0" END IFNEXTFOR A = 1 to LEN(compltemp$)mirrorcompltemp$ = mid$(compltemp$, a, 1) + mirrorcompltemp$NEXTFOR a = 1 to LEN(mirrorcompltemp$)IF MID$(mirrorcompltemp$, a, 1) = "1" THEN IF a = 1 THEN temp4 = temp4 + 1 ELSE temp4 = temp4 + 2 ^ (a-1) END IFEND NEXT
Statistics: Posted by Seb McClouth — Tue Feb 05, 2008 3:07 pm
Statistics: Posted by burger2227 — Tue Feb 05, 2008 2:47 pm
Statistics: Posted by Ralph — Tue Feb 05, 2008 11:03 am
Statistics: Posted by burger2227 — Tue Feb 05, 2008 5:28 am
Statistics: Posted by Seb McClouth — Tue Feb 05, 2008 1:06 am