Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using OR/XOR instead of adding/subtracting 1

Status
Not open for further replies.

Guggly

Programmer
Jan 18, 2004
110
US
Hi! This is probably a real dumb question, but what's the advantage to using OR and XOR for a bit addition/subtraction if I know what the additional value should be and I know that it hasn't already been applied to the original value?

In other words, if I want the second binary digit in 61 to be set, I can just add 2 (61 + 2) instead of using OR (61 OR 2), they both give the same answer, 63.

I understand that it would be safer to use OR in the event that you haven't evaluated whether or not that bit field is set yet (63 OR 2), but is that the only advantage it would give?

I'm just curious from a learning perspective. Thanks!
 
Using the bitwise operators (Or, And, Xor, Mod) is only really useful when you're dealing with power-of-two values. The classic 2,4,8,16,32, etc.

Using them gives you the ability to set or clear individual bits in a byte.
SET:[tt]
00010010 Or 00000100 = 00010110
18 Or 4 = 22
&h12 Or &h04 = &h16
[/tt]
CLEAR:[tt]
00010110 And Not 00000100 = 00010010
22 And Not 4 = 19
&h16 And Not &h04 = &h12
[/tt]
Using the binary arithmetic is more efficient than other methods, as the CPU is able to execute them directly.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
The one to watch for is if you don't know if a particular bit is set, or if you do the operation twice:
[tt]
61 Or 2 --> 63 61 + 2 --> 63
[/tt]
but if you repeat the operation (i.e bit is already set:
[tt]
63 Or 2 --> 63 63 + 2 --> 65
^ ^
Correct Bit 2 now cleared
[/tt]

The moral is use the operation that will do the job you want - bitwise operators for bitwise operations, arithmetic operators for arithmetic operations.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
The other issue relates to cases where you may need to set, reset, or test bit-fields that overlap the sign bit.

In general, when using bit-masking operations code them as such. Avoid using arithmetic for non-arithmetic operations.
 
Okay, thanks for your explanations, makes more sense now
 
Or and Xor are good for Setting Flag bits...

such as...
Code:
Const Flag1 = 1 '0001
Const Flag2 = 2 '0010
Const Flag3 = 4 '0100
Const Flag4 = 8 '1000

Dim MyFlags as Integer

If you set flag1 at some point in the program...
Code:
MyFlags = Flag1

MyFlags will = 0001

then later you decide to set it to Flag2
Code:
MyFlags = Flag2

it will erase Flag1 and it will now = 0010 (or 2)

to avoid this you have 2 options... +/or

it will work if you use + ...
Code:
MyFlags = MyFlags + Flag2

MyFlags now equals 3 (0011)

BUT...

if you decided to set Flag1 and Flag3 later on...
Code:
MyFlags = MyFlags + Flag1 + Flag3

it will try to add 0101 (5) and 0011 (3)
Code:
  0011  (3 - Flag1 & Flag2)
  0001  (1 - Flag1)
+ 0100  (4 - Flag3)
------
  1000  (8 - Flag4)

so it will result in only Flag4 being set, when Flags 1,2,& 3 should be set and Flag4 should not be...

To avoid this you want to use or...
Code:
MyFlags = MyFlags or Flag1 or Flag3

To get this...
Code:
   0011  (3 - Flag1 & Flag2)
or 0001  (1 - Flag1)
or 0100  (4 - Flag3)
------
   0111  (7 - Flag1 & Flag2 & Flag3)

Which is the correct result

----------------------------------------
For a quick reference:
Code:
3 and 5 =  1
3  or 5  = 7
3 xor 5 =  6

0011 and 0101 = 0001
0011  or 0101 = 0111
0011 xor 0101 = 0110

not 0001 ~ 1110
0011 and (not 0101)
  is the same as
0011 and 1010 = 0010

Good Luck ;-)

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top