I feel like there should be a simple bitwise operation that can accomplish this but for the life of me I can't think what it is:
final_value = do_invert ? !input : input
final_value = do_invert ^ input;
where ^ is the exclusive OR (XOR)
Related
I came across an algorithm that uses a lot of XOR shift like so:
std::uint16_t a = ...
std::uint16_t b = a ^ ( a >> 4 )
I read XOR is used for all kind of good stuff, like finding parity, determining odd/even counts etc. So I'm wondering: Does this operation (on its own) have a certain meaning? Is it a common pattern? Or is it just unique to this algorithm?
No I'm not talking about THE xorshift pseudo-number algorithm.
Let's take a look at what it produces given input 0xIJKL:
0xIJKL
^ 0x0IJK
--------
0xI???
Doesn't seem very meaningful to me by itself, but this pattern seems to be used as a sub step in many parity bit twiddles. For example, a twiddle for calculating parity bit of a word (from https://graphics.stanford.edu/~seander/bithacks.html):
unsigned int v; // word value to compute the parity of
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1;
I'm working on a gameboy emulator. One of the CPU operations I need to implement is the adding of a byte n to the stack pointer sp (opcode E8). The carry flag needs to be set if there is a carry from bit 7. I've looked at two implementations for this operation and they both follow the same carry detection logic. The code for this is roughly as follows:
int result = (sp + n) & 0xFFFF
boolean carry = ((sp ^ n ^ result) & 0x100) != 0
I have worked through this logic with a few examples and it does work, but I simply don't get how it works. I understand how xor works but what's the logic behind its application here? Thanks.
Addition can be written as:
a + b = a ^ b ^ (c << 1)
Where c is the carry-out for every bit (c << 1 is the carry-in). This can also be used as a way to implement addition.
Therefore if the a ^ b part is XORed out of the sum again, we're left with c << 1. Bit 8 of that is the carry-out of bit 7.
I am trying to understand dbns code in foam-extend. But I am having bit of doubt in a specific part of the following code given below.
deltaRLeft & gradrho[own] or
deltaRRight & gradU[nei]
I thing & used here is a reference operator, but if any one can explain it in more detail, it will helpful for me.
Flux::evaluateFlux
(
rhoFlux_[faceI],
rhoUFlux_[faceI],
rhoEFlux_[faceI],
rho_[own] + rhoLimiter[own]*(deltaRLeft & gradrho[own]),
rho_[nei] + rhoLimiter[nei]*(deltaRRight & gradrho[nei]),
U_[own] + cmptMultiply(ULimiter[own], (deltaRLeft & gradU[own])),
U_[nei] + cmptMultiply(ULimiter[nei], (deltaRRight & gradU[nei])),
T_[own] + TLimiter[own]*(deltaRLeft & gradT[own]),
T_[nei] + TLimiter[nei]*(deltaRRight & gradT[nei]),
R[own],
R[nei],
Cv[own],
Cv[nei],
Cp[own],
Cp[nei],
Sf[faceI],
magSf[faceI]
);
What is the & exactly doing here, if it can be explained in detail.
The part of the code is from dbns/numericFlux/numericFlux.C
It's the bitwise and operator.
It compares each bit of the first operand to the corresponding bit of the second operand.
If both bits are 1, the result bit is set to 1 otherwise 0.
As example:
11001001
& 10111000
--------
= 10001000
I am afraid that the first and second answers are mostly not applicable within the OpenFOAM context.
In the OpenFOAM context, & is the inner product if the operands are tensors. Thereat, deltaRRight and gradT[nei] are in fact tensor objects.
Please look at the OpenFOAM's programmers' guide, Section 1.4.1 and 1.3.1.
There are two different & operators.
The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Both operands to the bitwise AND operator must be of integral types.
For example:
#include <iostream>
using namespace std;
int main() {
unsigned short a = 0xFFFF; // pattern 1111 ...
unsigned short b = 0xAAAA; // pattern 1010 ...
cout << hex << ( a & b ) << endl; // prints "aaaa", pattern 1010 ...
}
DWORDLONG index = ((((DWORDLONG) i.nFileIndexHigh) << 32) | i.nFileIndexLow);
Given index, I want to find out what the components of i.fileindexhigh and i.fileindexlow are. Is it possible? A general idea would be helpful.
Yes, its possible, you just need to do the inverse operation of what you posted: instead of << and |, >> and &,
nFileIndexLow = index & 0x00000000FFFFFFFF;
nFileIndexHigh = index >> 32;
Consider researching on bitwise operations, or at least put your calculator in hexadecimal/binary mode and play with masks and shifts.
i.nFileIndexHigh == index >> 32;
i.nFileIndexLow == index & 0x00000000FFFFFFFF;
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