How to perform bitwise OR and NOT scalar operations in AWS Redshift? - bit-manipulation

AWS Redshift has bitwise &, <<, >> scalar operatos for AND, SHIFT LEFT, and SHIFT RIGHT but what about the bitwise OR and NOT?
Look at https://docs.aws.amazon.com/redshift/latest/dg/r_OPERATOR_SYMBOLS.html
Is there exist any way to perform it?

For bitwise OR you can use
(column_name) | (column_name)
For bitwise NOT you can use
~(column_name)
If I’ve made a bad assumption please comment and I’ll refocus my answer.

Related

Evaluation with bitwise operation

When checking whether n is a 2's exponential number, I wrote the following line:
if(n&(n-1)!=0)
to my surprise, when n is 6, this is evaluated to false. Then if I change it to
if((n&(n-1))!=0)
I get the correct result.
EDIT:
I get it that != is evaluated first, but I still don't see how the result is false in the case where n = 6.
5!=0 is true
then 6&true should still be true right?
In expression you originally used, (n-1) != 0 is executed first, then n & is applied to its result.
At the first sight it might not look logical, but == and != have higher precedence than & or |.
It's a common C/C++ behavior and it has been already addressed by its criticism:
Operators in C and C++: Criticism of bitwise and equality operators precedence
Conceptually, operators | and & are like + and *, so having them with low precedence is not very intuitive and leads to questions like the one you posted.
Hint: in contact with any programming language, it is good to develop a habit of checking operator precedence topic whenever in doubt. Actually, you will find quite soon that you actually memorized it in the meantime.
I would say the order of precedence is very important in here. From your second attempt the result is correct as this is called operator precedence (or "order of operations"). Precedence of common operators is generally defined so that "higher-level" operations are performed first (i.e., advanced operations "bind more tightly")/ For simple expressions, operations are typically ordered from highest to lowest in the order:
Parenthesization
Factorial
Exponentiation
Multiplication and division
Addition and subtraction
A common pitfall is that inner ( that cause to change the whole balance. Same concept, however bit-twise operations have the lowest priorities.

bitwise operations in Eigen

It doesn't seem like Eigen supports bitwise operations.
I would like bitwise SIMD functionality for "shift left" and "and".
Is there a quick and dirty way to implement this functionality? Can i call an intrinsic function and input it with something like Eigen vector.data()?
I think Eigen don't support this because that there isn't a good way to do this for float, double and complex numbers.
You can always override those C++ bitwise operators by yourself, taking two MatrixBase as parameters.
For bitwise assignment operators, you need to add a function inside MatrixBase class.
Eigen made this possible, see here how to.

What is the name of this operator "^"?

I know that this operator does the "AND logical operator" but I don't know how to deal with it and if it deals with decimal numbers or just binary numbers ?
It is the the XOR operator:
XOR (Exclusive Or)
This operation is performed between two bits (a and b). The result is
1 if either one of the two bits is 1, but not in the case that both
are. There for, if neither or both of them are equal to 1 the result
is 0.
That is the bitwise XOR operator.
It performs an exclusive or operation.
It is not the logical AND, it's the bitwise XOR. It operates on integers, which are always binary numbers in C++, and may be overloaded for other types.
That's the bitwise XOR operator (so not logical, not AND) and decimal numbers are represented as binaries, so of course it works for them as well.
The logical AND is && and the bitwise AND is &.

What is >>> operation in C++

In this blog post the author has suggested the following as the bug fix:
int mid = (low + high) >>> 1;
Does anyone know what is this >>> operator? Certainly its not there on the following operator reference list:
http://msdn.microsoft.com/en-us/library/x04xhy0h%28v=vs.71%29.aspx
http://www.cplusplus.com/doc/tutorial/operators/
What is it and how does that solve the overflow problem?
>>> is not a part of C++. The blog contains code in Java.
Check out Java online tutorial here on Bitwise shift operators. It says
The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
>>> is the logical right shift operator in Java.
It shifts in a zero on the left rather than preserving the sign bit. The author of the blog post even provides a C++ implementation:
mid = ((unsigned int)low + (unsigned int)high)) >> 1;
... if you right-shift unsigned numbers, preserving the sign bit doesn't make any sense (since there is no sign bit) so the compiler obviously uses logical shifts rather than arithmetic ones.
The above code exploits the MSB (32rd bit assuming 32 bit integers): adding low and high which are both nonnegative integers and fit thus into 31 bits never overflows the full 32 bits, but it extends to the MSB. By shifting it to the right, the 32 bit number is effectively divided by two and the 32rd bit is cleared again, so the result is positive.
The truth is that the >>> operator in Java is just a workaround for the fact that the language does not provide unsigned data types.
The >>> operator is in a Java code snippet, and it is the unsigned right shift operator. It differs from the >> operator in its treatment of signed values: the >> operator applies sign extension during the shift, while the >>> operator just inserts a zero in the bit positions "emptied" by the shift.
Sadly, in C++ there's no such thing as sign-preserving and unsigned right shift, we have only the >> operator, whose behavior on negative signed values is implementation-defined. To emulate a behavior like the one of >>> you have to perform some casts to unsigned int before applying the shift (as shown in the code snippet immediately following the one you posted).
The Java expression x >>> y is more or less equivalent to the C++ expression unsigned(x) >> y.
>>> is not C++ operator. I think it's an operator in Java language. I'm not sure though!
EDIT:
Yes. That is java operator. Check out the link to the article you provided. The article is using Java language!
It is a java operator, not related to C++.
However all the blog author does is change the division by 2 with a bit-wise right shift (i.e. right shifting the value by 1 is similar to dividing by 2 ^ 1).
Same functionality, different machine code output (bit shifting operations are almost always faster than multiplication/division on most architectures).

Represent BitWise AND,OR,SHIFT Operations in PseuodoCode

How Can I represent bitwise AND, OR Operations and Shift Operations using PsuedoCode?
Please Hep me
You said bitwise, so why not & (AND) | (OR) and << (left shift) or >> (right shift)?
Fairly universal C-style syntax.
If you want to represent implementations of bitwise operations, you can introduce "bit enumerator" (some kind of object to iterate over that will emit bits from the less significant one to the most significant for given number) and use mapping for arguments to result (like AND and OR definition suggests).