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 &.
Related
reverse a given number only using bitwise operation
e.g.:
input: 4532
output : 2354
I'm not able to think of any bitwise operations to tackle this question.Any solution/assistance would be of great help.
The division and modulo by 10 needs to be performed using bitwise operations only.
there's no efficient way of reversing a number using only bitwise operators (mostly shift operators). But we can use BCD conversion to convert the integer to BCD and then reverse using shift operators. So after BCD conversion we can shift every set of 4 bits (representing a digit ranging from 0-9) step wise in the opposite order, to get the reversed Number in BCD format.
BitField1 = 00100110
BitField2 = 00110011
((BitField1 & ~BitField2) | (BitField2 & ~BitField1)); = 00010101
So this is the long version of a common bitwise operation, what is it?
Want to understand if above bit operation is some known operation ?
This is XOR. You end up with a 1 in those bits where either BitField1, or BitField2, but not both, have a 1.
As Wikipedia says, one use is
"Assembly language programmers sometimes use XOR as a short-cut to setting the value of a register to zero. Performing XOR on a value against itself always yields zero."
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.
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).
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).