PDA

View Full Version : bitwise operators



sandyrepope
12-03-2007, 06:40
Do the bitwise operators work?

I'm having a little trouble using them.

Help, Anyone?

Thanks
Sandy

ErosOlmi
12-03-2007, 08:52
Hi Sandy,

can you please post an example not working?
Sometimes it is very difficult to distinguish bitwise from logical so a little example can help me better understand the situation.
I have the impression I need to develop specific functionality for bitwise

Ciao
Eros

sandyrepope
12-03-2007, 14:34
I'm sorry that I wasn't very clear. I have problems with that sometimes. Actually, what I need to know is how to tell if I'm getting the correct result. Give me some time and I'll have an example worked up.

Thanks
Sandy

ErosOlmi
12-03-2007, 15:52
Sandy,

please have a look at this example about bitwise operations.
In any case I've seen that I can add new functionalities for future thinBasic version to set or get specific bits.

Let me know if you have any example of code giving strange results and I will look at it immediately.

Ciao
Eros


uses "console"

dim a,b,c as long

b = &H09700
c = &H03FFF

a = b and c
PrintResults("--- AND operation: a = b and c", a, b, c)

a = b or c
PrintResults("--- OR operation: a = b or c", a, b, c)

a = not b
PrintResults("--- NOT operation: a = not b ", a, b, c)

console_waitkey

function PrintResults(Operation as string, a as long, b as long, c as long)
console_writeline Operation
console_writeline "b = " & BIN$(b, 32) & " " & hex$(b, 4)
console_writeline "c = " & BIN$(c, 32) & " " & hex$(c, 4)
console_writeline string$(40, "-")
console_writeline "a = " & BIN$(a, 32) & " " & hex$(a, 4)
console_writeline ""
end function


Results should be this one:

--- AND operation: a = b and c
b = 00000000000000001001011100000000 9700
c = 00000000000000000011111111111111 3FFF
----------------------------------------
a = 00000000000000000001011100000000 1700

--- OR operation: a = b or c
b = 00000000000000001001011100000000 9700
c = 00000000000000000011111111111111 3FFF
----------------------------------------
a = 00000000000000001011111111111111 BFFF

--- NOT operation: a = not b
b = 00000000000000001001011100000000 9700
c = 00000000000000000011111111111111 3FFF
----------------------------------------
a = 11111111111111110110100011111111 68FF

kryton9
12-03-2007, 22:06
I know there is lots of power in bitwise operators, but what is the common use for them?

ErosOlmi
12-03-2007, 22:16
How to reply to your question Ken?

Bits are ALL in computer world. Any time you do any action on a computer, you are mainly switching on/off some million bits per milliseconds.

Going to programming side, you can use them to switch on/off some property for example.
A practical usage is when you use DIALOG NEW (http://www.thinbasic.com/public/products/thinBasic/help/html/dialog_new.htm) and set some window style or extended style. Using the OR operator you are switching on some bits in a DWORD variable telling windows to activate a property.

There can be so many ways you can use them. I will see for next thinBasic version to add some interesting functions to set on/off specific bits in interger class variables (BYTE, INTEGER, LONG, DWORD, QUAD) and to check if specific bits are on/off.

Maybe someone else can be more precise and explanation rich than me on this aspect, I'm not a binary expert ??? :D

Ciao
Eros

kryton9
13-03-2007, 00:28
Thanks Eros, that alone is very useful. Those types of long parameter combos in one or'd statement is fascinating.
I know in game programming it is used, I have seen it in tutorial codes, but did not apply them yet.

When I run across more info I will post about it here too.

RobertoBianchi
13-03-2007, 10:08
Use a bit within an interpreter seems a non sense because a script isn't a space saver program, in fact normally the interpreter loads and use more memory and processor resources than a compiled program (of course it depends of the compiler and the interpreter ;D) however often the APIs demand to set up or to identify the bit value. See the GetVersionEx() function just for an example.

Regards,
Roberto

kryton9
14-03-2007, 04:43
THanks Roberto, will check that out.