More handy information can be found here;
http://en.wikipedia.org/wiki/Bitwise_operation
It takes a little bit to get your head around the concepts, but eventually all starts to make sense sooner than later. But unfortunately I can't find too many DarkBASIC Pro examples of binary/bitwise operations; if anyone else has some simple & useful examples or tutorials, by all means, please post them.
Currently, I'm starting off with the small step of showing a variable as a binary byte... but can't get it to work.
flags As Byte
Do
CLS
Set Cursor 0,0
flags = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)
Print rightkey(), leftkey(), downkey() , upkey()
Print flags
`How would I display flags as binary, not dec?
`Print %flags
Loop
EDIT: Oh oh, halfway there!
Sync On
flags As Byte
Do
CLS
Set Cursor 0,0
flags = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)
Print rightkey(), leftkey(), downkey() , upkey()
Print flags
Print RIGHT$(BIN$(flags),4)
Sync
Loop
That kinda works! ARK, GETTING OVER STIMULATED. CODING IS AWESOME!
DOUBLE EDIT:
Ok, now I'm stuck. My code is as per below.
Rem Project: Dark Basic Pro Project
Rem Created: Wednesday, July 14, 2010
Rem ***** Main Source File *****
Sync On
flags1 As Byte
flags2 As Byte
flags3 As Byte
Do
CLS
Set Cursor 0,0
flags1 = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)
Print "flags1: ", flags1
Print "Key Press: ", rightkey(), leftkey(), downkey() , upkey()
Print "Bin$ flags1: ", RIGHT$(BIN$(flags1),4)
Print " "
Print "<< 1 is now being applied to flags1, to create an LSHIFT flags2"
Print "But note how flag 2 increases from 4 bits to 5 bits after applying 1 LSHIFT"
flags2 = (flags1 << 1)
Print " "
Print "flags 2: ", flags2
Print "Bin$ flags 2: ", Right$(BIN$(flags2),5)
Print " "
`flags3 = RIGHT$(BIN$(flags2),4)
Print "Flag 3 should = Flag 1 shuffled to the left, yet restricted a dec of 16"
Print "and a max of 4 bits. Not too sure how to do this..."
Print " "
Print "flags 3: ", flags3
Print "Bin$ flags 3: ", Right$(BIN$(flags3),4)
Sync
Loop
Any ideas?
ZOMG IT'S A TRIPLE EDIT!
I did some reading last night and this morning. I decided to focus on what exactly a bit is, and how it plays with bytes & words. May have solved my complication.
Sync On
flagByte1 As Byte
flagByte2 As Byte
flagByte3 As Byte
flagWORD4 As Word
Do
CLS
Set Cursor 0,0
flagByte1 = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)
Print "Key Press: ", rightkey(), leftkey(), downkey() , upkey()
Print " "
Print "flagByte1: ", flagByte1
Print "Bin$ flagByte1: ", RIGHT$(BIN$(flagByte1),8)
Print " "
flagByte2 = (flagByte1 << 1)
Print "flagByte2 = (flagByte1 << 1)"
Print " "
Print "flagByte2: ", flagByte2
Print "Bin$ flagByte2: ", RIGHT$(BIN$(flagByte2),8)
Print " "
flagByte3 = (flagByte1 << 6)
Print "flagByte3 = (flagByte1 << 6)"
Print " "
Print "flagByte3: ", flagByte3
Print "Bin$ flagByte3: ", RIGHT$(BIN$(flagByte3),8)
Print " "
flagWORD4 = (flagByte1 << 14)
Print "flagWORD4 = (flagByte1 << 14)"
Print " "
Print "flagWORD4: ", flagWORD4
Print "Bin$ flagWORD4: ", RIGHT$(BIN$(flagWORD4),16)
Sync
Loop
Now, what to do with my newly found binary powers!
Mer-mer-mer-MULTI-EDIT!
Ok, this one's done and dusted to me. Time to move onto increasing my logic skills.
Rem Project: Dark Basic Pro Project
Rem Created: Wednesday, July 14, 2010
Rem ***** Main Source File *****
Set Display Mode 640, 480, 32, 0, 0, 0
Sync On
Sync Sleep 1
flagByte1 As Byte
flagByte2 As Byte
flagByte3 As Byte
flagWORD4 As Word
NOTbyte1 As Byte
ANDbyte1 As Byte
Do
CLS
ink Rgb(160,160,255), Rgb(60,60,60)
Set Cursor 0,0
Print " "
flagByte1 = (upkey() || downkey() << 1 || leftkey() << 2 || rightkey() << 3)
Set Text Font "Wingdings",1
Select FlagByte1
Case %00000001
Print "á"
EndCASE
Case %00000010
Print "â"
EndCASE
Case %00000100
Print "ß"
EndCASE
Case %00001000
Print "à"
EndCASE
Case Default
Set Text Font "System"
Print "Alternate"
Endcase
ENDSELECT
Set cursor 1, 50
Set Text Font "System",1
Print "Key Press: ", rightkey(), leftkey(), downkey() , upkey()
Print " "
Print "flagByte1: ", flagByte1
Print "Bin$ flagByte1: ", RIGHT$(BIN$(flagByte1),8)
Print " "
flagByte2 = (flagByte1 << 1)
Print "flagByte2 = (flagByte1 << 1)"
Print " "
Print "flagByte2: ", flagByte2
Print "Bin$ flagByte2: ", RIGHT$(BIN$(flagByte2),8)
Print " "
flagByte3 = (flagByte1 << 6)
Print "flagByte3 = (flagByte1 << 6)"
Print " "
Print "flagByte3: ", flagByte3
Print "Bin$ flagByte3: ", RIGHT$(BIN$(flagByte3),8)
Print " "
flagWORD4 = (flagByte1 << 14)
Print "flagWORD4 = (flagByte1 << 14)"
Print " "
Print "flagWORD4: ", flagWORD4
PrintBIN(flagWORD4,16)
Print " "
NOTByte1 = flagByte1..0
PrintBIN(NOTByte1,8)
Print " "
ANDByte1 = flagByte1 && flagByte2
PrintBIN(ANDByte1,8)
Sync
Loop
function PrintBIN(Y,X)
Print "Result: ", RIGHT$(BIN$(Y), X)
Endfunction