LoginLogin

Decimal -> Binary

Root / Programming Questions / [.]

MariominerCreated:
So I was programming something and found a need to be able to convert decimal numbers into binary numbers. I though "oh, it must be similar to HEX$()", so I looked at the list of functions. However there was no BIN$() or whatever. So is there any way to do this with a simple command or do I have to do it through a method I learned a while ago? I know how to turn binary into decimals via &B, but I need to go vice-versa.

Uh? you mean you have to display the binary values? &B is just the only way to write binary numbers directly , just like &H specifies hexadecimal values.

No, I just need a way to turn say 48 into "110000" to make sure that (112 and 48)!=48. That is messing my program up. Or, now that I think of it, is there any way to simply filter out certain bits, like "101110" filtering out into just "01110" to check?

you don't need binary numbers to use bitwise operators. ? 112 AND 48 ? &H70 AND &H30 ? &H70 AND 48 ? 112 AND &B110000 all lead to the same result

The only reason you might want to get a binary string is to display it to the user for whatever reason. This might work (can't test it currently):
DEF BIN$(K)
VAR H$ = HEX$(K)
VAR O$ = ""
FOR I=0 TO LEN(H$)-1
    K = VAL("&H" + H$[I])
    IF K AND 8 THEN O$ = O$ + "1" ELSE O$ = O$ + "0"
    IF K AND 4 THEN O$ = O$ + "1" ELSE O$ = O$ + "0"
    IF K AND 2 THEN O$ = O$ + "1" ELSE O$ = O$ + "0"
    IF K AND 1 THEN O$ = O$ + "1" ELSE O$ = O$ + "0"
NEXT
WHILE O$[0] == "0"
    O$ = RIGHT$(O$, LEN(O$)-1)
WEND
RETURN O$
END

I know that I don't need to use binary for bitwise operators, but it is preferable. For example...
VAR C$
C$=BIN$(48) 'THIS WILL BE 110000 IF I REMEMBER
VAR B$
B$=BIN$(112) 'THIS WILL BE 1110000 IF I REMEMBER RIGHT
PRINT 48 AND 112 '48, IT PASSES NORMALLY
IF LEN(B$)!=LEN(C$) THEN
 IF LEN(C$)<LEN(B$) THEN C$=" "*(LEN(B$)-LEN(C$))+C$
 IF LEN(C$)>LEN(B$) THEN B$=" "*(LEN(C$)-LEN(B$))+B$
ENDIF
VAR YAY$
FOR I=0 TO LEN(B$)-1
 IF MID$(B$,I,1)==MID$(C$,I,1) THEN YAY$=YAY$+MID$(B$,I,1) ELSE YAY$=YAY$+"0"
NEXT
PRINT VAL("&B"+YAY$) 'SHOULD BE 32
I think that this example is right. Anyway, point is, it is different (unless I did that wrong, that would be stupid of me) and useful for not just display. If I did it wrong, then I guess I need another solution. EDIT: WAIT THIS IS HORRIBLY WRONG. I'll make another post showing what I actually want to do.

Okay, so that last post is NOT what I would do. I'll show it now.
DEF CHECKBITS(C$,B$,BITS)
 PRINT VAL("&B"+C$) AND VAL("&B"+B$) 'IE IF C$=BIN$(48) AND B$=BIN$(112), IT IS 48
 IF RIGHT$(C$,BITS)==RIGHT$(B$,BITS) THEN RETURN TRUE ELSE RETURN FALSE
 'RETURN TRUE IF ONLY 'BITS' BITS ARE THE SAME.
END
So I would use this to check whether or not a BGGET() is returning the same character with different rotations or whatever. Or is there already a way to do that and I'm just clueless?

I would do something like this:
DEF CHECKBITS(C%,B%,MASK%)
 RETURN ((C% XOR B%) AND MASK%)==0
END
In the case you've described, MASK% might be 1023 (aka &H3FF or (1<<10)-1) to check the bottom 10 bits. No need to use strings.

Woah, I never even considered using XOR. Thanks calc84maniac! I guess I don't need to do this system.

Not to detract from calc's solution, but this would also work and, IMO, is more readable:
DEF CHECKBITS(C%,B%,MASK%)
 RETURN (C% AND MASK%) == (B% AND MASK%)
END