~ Operation

Blitz3D Forums/Blitz3D Beginners Area/~ Operation

_PJ_(Posted 2010) [#1]
Does anyone know where the ~ operator can be useful? Perhaps in faster calculations?


chi(Posted 2010) [#2]
itīs called "bitwise negation"
this takes the value given and switches all the binary 1s to 0s and 0s to 1s

Print Bin(1)
Print Bin(~1)



GfK(Posted 2010) [#3]
itīs called "bitwise negation"
...or XOR, if you prefer.


jfk EO-11110(Posted 2010) [#4]
Excuse me for being a smarta**, but I think this isn't XOR, but NOT.

The bitwise, boolean NOT Operator is especially useful to "kill" a certain Bit, eg. let's say you want to erase the Bit 3 (value 8) in a certain variable:

a = a and ( ~ 8)

where setting the same bit to true would work this way:

a = a or 8

Yes, it should be pretty fast. I don't know if Blitz3D is converting the BASIC ~ command directly into the Machine Code "NOT" (this command is a true CPU Assembler command, even one of the faster ones) or if there are some additional savity checks, but even then it should be very fast. These commands ore often used to edit Bitmaps. For example you can mix two Pixels with the OR command(edit: actially I think this isn't correct, mixing would be (a+b)/2), or invert a bitmap with the ~ command. There's even something called BitBlit, hardware-accelerated boolean Bitmap manipulation.

the boolean operators should also be used to work with ReadPixelfast etc.:

rgb=readpixelfast(x,y) AND $FFFFFF
red=(rgb and $FF0000) shr 16
and so on.

XOR at the other hand is "exclusive OR", means, the result bit is only true when eighter sourcebit 1 is true and bit 2 is false, or Bit 1 is false and Bit 2 is true. It means, the result is true, if bit 1 is true or bit 2 is true, but not both of them (hence exclusive OR). Therefor you can make your own XOR this way:

result= (a And (~b)) Or (b And (~a))

So, try:
a=3
b=13

Print Bin$(a)
Print Bin$(b)
Print Bin$(a Xor b)
Print Bin$((a And (~b)) Or (b And (~a)))

So you see. AND, XOR, NAND, NOT, OR are all made of AND, OR and NOT. These functions can be tracked down to some very basic microchips, where you can use 2 Pins as Inputs and one Pin as Output, doing something like a NAND operation very close to a simple NPN Transistor element. In fact you can build your NAND processor with some simple Transistors.


Floyd(Posted 2010) [#5]
...or XOR, if you prefer.

That's a mistake I've also made.

The ~ is Bitwise Not. In BlitzMax it is also Bitwise XOR.


jfk EO-11110(Posted 2010) [#6]
I was rather surprised that GfK (Blitz Vet) was wrong about that. So this is the explanation.


Charrua(Posted 2010) [#7]
hi, most 8 bit microcontrollers has bit instructions, set, reset, toggle, etc, most microprocessors don't have bit instructions so, bit manipulation is made from Logical operations

set: Variable OR Constant (note 1)
reset: Variable And Constant (note 2)
toggle: Variable Xor Cosntant (note 3)


note1: bits we want to set 1, the rest 0
note2: bits we want to reset 0, the rest 1
note3: bits we want to toggle 1, the rest 0

when using Flags in a variable these instructions give us the posibility of manipulate them individually, but they aren't so natural to use, we have to do some effort to understand them.

languajes normally give us And, Or, Not and we use them with logical or boolean varibles, and &, |, ~ as bitwise operators, normally i test both to see any diferences, and most of the times are the same. Next time i needed, i have to test again, becaus normally i forget the results.

Juan