Page 1 of 1

Difference between Bitwise External/Internal Inclusive OR?

Posted: Thu Jun 11, 2009 10:43 pm
by XianForce
I've seen these two operators in code here and there... but I've never been able to find their differences/specialties, nor did a google search help much. So does anyone have something to enlighten me on these two operators?

Re: Difference between Bitwise External/Internal Inclusive OR?

Posted: Sun Jun 21, 2009 2:05 am
by Ginto8
XianForce wrote:I've seen these two operators in code here and there... but I've never been able to find their differences/specialties, nor did a google search help much. So does anyone have something to enlighten me on these two operators?
please rephrase and show us the symbols used for them =P I don't remember all the programming jargon. If by external inclusive or you mean the ~ symbol, it does a boolean NOT on each of the individual bits of the expression. | does a boolean OR between the bits of 2 expressions. & is like |, just AND instead of OR. ^ is XOR.
examples:
Click here to see the hidden message (It might contain spoilers)

Code: Select all

NOT:
var | returned
0    | 1
1    | 0

OR:
var1 | var2 | returned
0    | 1    | 1
0    | 0    | 0
1    | 0    | 1
1    | 1    | 1

AND:
var1 | var2 | returned
0    | 1    | 0
0    | 0    | 0
1    | 0    | 0
1    | 1    | 1

XOR:
var1 | var2 | returned
0    | 1    | 1
0    | 0    | 0
1    | 0    | 1
1    | 1    | 0

And I've just explained pretty much all of C/++'s bitwise operators.

Re: Difference between Bitwise External/Internal Inclusive OR?

Posted: Sun Jun 21, 2009 8:28 am
by dandymcgee
This seems like a good topic to ask what bitwise operators are most commonly (or not so commonly) used for. Has anyone discovered any neat little tricks using bit manipulation?

Re: Difference between Bitwise External/Internal Inclusive OR?

Posted: Sun Jun 21, 2009 4:31 pm
by MarauderIIC
dandymcgee wrote:Has anyone discovered any neat little tricks using bit manipulation?
There's quite a few, here: http://aggregate.org/MAGIC/
Some of which are:
Is Power of 2

A non-negative binary integer value x is a power of 2 iff (x&(x-1)) is 0 using 2's complement arithmetic.
Integer Constant Multiply

Given an integer value x and an integer or floating point value y, the value of x*y can be computed efficiently using a sequence derived from the binary value of x. For example, if x is 5 (4 + 1):

y2 = y + y;
y4 = y2 + y2;
result = y + y4;

In the special case that y is an integer, this can be done with shifts:

y4 = (y << 2);
result = y + y4;
Integer Power

Given an integer value x and an integer or floating point value y, the value of y to the x power can be computed efficiently using a sequence derived from the binary value of x. For example, if x is 5 (4 + 1):

y2 = y * y;
y4 = y2 * y2;
result = y * y4;

Re: Difference between Bitwise External/Internal Inclusive OR?

Posted: Sun Jun 21, 2009 4:37 pm
by teamtwentythree
A fairly obvious use. I use bitwise operators to store/retrieve booleans from bytes when sending data over the "tubes". I also use bit shifting in case I'm mixing data in one byte (Like a 4-bit int + 4 1-bit bools)