I have the following line of code:
contents[pos++] ^= key[shift++];
What does operator ^= mean?
It is the XOR assignment operator. Basically:
x ^= y;
is the same as:
x = x ^ y;
This means preform an XOR operation on contents[pos++] using key[shift++] and set contents[pos++] equal to the result.
Example:
contents[pos++] 00010101
key[shift++] 10010001
--------
10000100
It is a bitwise XOR operator.
x ^= y
is basically
x = x ^ y
of course, this is a bitwise operation
http://en.wikipedia.org/wiki/Bitwise_operation
It is a bitwise exclusive OR on two integers. http://bytes.com/topic/c/answers/726626-what-caret-qualifier
Related
I'm trying to replicate the function of a loop using only bitwise and certain operators including ! ~ & ^ | + << >>
int loop(int x) {
for (int i = 1; i < 32; i += 2)
if ((x & (1 << i)) == 0)
return 0;
return 1;
}
Im unsure however how to replicate the accumulating nature of a loop using just these operators. I understand shifting << >> will allow me to multiply and divide. However manipulation using ! ~ & ^ ~ has proven more difficult. Any Tips?
http://www.tutorialspoint.com/cprogramming/c_operators.htm
Edit:
I understand how the addition of bits can be achieved, however not how such an output can be achieved without first calling a while or for loop.
Maybe this can help:
int loop(int x) {
x = x & 0xaaaaaaaa; // Set all even numbered bits in x to zero
x = x ^ 0xaaaaaaaa; // If all odd numbered bits in x are 1, x becomes zero
x = !x; // The operation returns 1 if x is zero - otherwise 0
return x;
}
Your code tests all odd bits and returns 1 if all of them are set. You can use this bitmask: ...0101 0101 0101
Which, for 32 bits is 0xAAAAAAAA.
Then you take your value und bitwise-and it. If the result is the same as your mask, it means all bits are set.
int testOddBits(int x) {
return (x & 0xAAAAAAAA) == 0xAAAAAAAA;
}
The following statements in C:
iONE >>= iShift;
iONE &= 0xffefffff;
iONE |= (((((long)(*temp & 0x7f) - 65) << 2) + iShift + 1023) << 20) | (iTWO & 0x80000000);
Is there something like shr=, And= and Or= in Delphi. Is there more appropriate way than literal porting, especially the third statement.
Those are basically two operations combined.
x >>= y means x = x >> y.
The same applies for &= and |=.
And for the Delphi part: And, Or, and Right Shift.
I am a complete beginner at bitwise operations (and not very experienced at C either) and I bumped into the expression:
x |= (1<<y)
At first I thought it meant "x equals x or y shifted left by on bit", but then I realized that would be:
x |= (y<<1)
Lastly I thought it meant "x equals x or 1 shifted left by y bits", but I don't understand where that 1 is in an 8-bit register, does it mean 00000001? so that:
a = 2
b = 1<<a // so b=00000010
Could someone tell me the correct meaning of this statement. Also, if anyone has a good link explaining bitwise syntax I'd be grateful.
Thanks.
x |= ...
is shorthand for
x = x | ...
It assigns the value of x | ... to x.
1 << y
is 1 left-shifted by y. E.g.
00000001 << 1 -> 00000010
So,
x |= (1 << y)
is OR x with 1 left shifted by y (and assign the result to x).
In other words, it sets the y'th bit of x to 1.
x = 01010101
x |= (1 << 1) -> 01010111 (it set the 2nd bit to 1)
The first statement means left shift the binary representation of 1 (0b0000001) by y bits. Then OR the value with X.
The assumption is correct for the second statement.
The third statement will yield 4 (0b0000000100).
In terms of bit operation semantics the C standard defines all bit operations to represented such that binary numbers are read right to left with ascending values of powers of 2. You do not need to worry about endianess or two complements etc, the compiler will handle that for you. So (0b00100) = 4, (0b000010) = 2, (0b00001) = 1, and so on.
What is the meaning of the >>= symbol in C or C++? Does it have any particular name?
I have this for loop in some CUDA code which looks like this
for(int offset=blockDim.x; offset>0; offset >>=1)
{
//Some code
}
How does the offset variable get modfied with the >>= operator?
The >>= symbol is the assignment form of right-shift, that is x >>= y; is short for x = x >> y; (unless overloaded to mean something different).
Right shifting by 1 is equivalent to divide by 2. That code looks like someone doesn't trust the compiler to do the most basic optimizations, and should be equivalent to:
for( int offset = blockDim.x; offset > 0; offset /= 2 ){ ... }
More information about bitwise operations here:
http://en.wikipedia.org/wiki/Binary_shift#Bit_shifts
Literally offset = offset >> 1, that is, offset divided by 2
That's the assignment version of right shift:
foo >>= 2; // shift the bits of foo right by two places and assign the result to foo
it's a bitwise shift right operator. it shifts the bits of the variable to right by the value of right operand.
What does the |= operator mean in C++?
Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same:
a = a | b;
a |= b;
The '|=' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.
The big advantage of the '|=' operator is when 'a' is itself a complex expression:
something[i].array[j]->bitfield |= 23;
vs:
something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;
Was that difference intentional or accidental?
...
Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:
something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;
Similar comments apply to all of the compound assignment operators:
+= -= *= /= %=
&= |= ^=
<<= >>=
Any compound operator expression:
a XX= b
is equivalent to:
a = (a) XX (b);
except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.
x |= y
same as
x = x | y
same as
x = x [BITWISE OR] y
It is a bitwise OR compound assignment.
In the same way as you can write x += y to mean x = x + y
you can write x |= y to mean x = x | y, which ORs together all the bits of x and y and then places the result in x.
Beware that it can be overloaded, but for basic types you should be ok :-)