I was having a look over this page: http://www.devbistro.com/tech-interview-questions/Cplusplus.jsp, and didn't understand this question:
What’s potentially wrong with the following code?
long value; //some stuff value &= 0xFFFF;
Note: Hint to the candidate about the base platform they’re developing for. If the person still doesn’t find anything wrong with the code, they are not experienced with C++.
Can someone elaborate on it?
Thanks!
Knowledge of binary number system and bit manipulation is less important in coding interviews as most Software Engineers do not have to deal with bits, which is more commonly used when dealing with lower level systems and programming languages.
Bit manipulation, in some cases, can obviate or reduce the need to loop over a data structure and can give many-fold speed-ups, as bit manipulations are processed in parallel, but the code can become more difficult to write and maintain.
For most other tasks, modern programming languages allow the programmer to work directly with abstractions instead of bits representing those abstractions. Bit manipulation can obviate or reduce the need to loop over a data structure and can speed up coding as bit manipulations are processed in parallel.
It depends on the interviewer and what they chose to focus on. Thinking back to my interview at current $DAYJOB , I don't recall doing much with bitwise operations, but I do know we ended up talking about bitstreams in one interview segment, and accessing register bits in another.
Several answers here state that if an int
has a width of 16 bits, 0xFFFF
is negative. This is not true. 0xFFFF
is never negative.
A hexadecimal literal is represented by the first of the following types that is large enough to contain it: int
, unsigned int
, long
, and unsigned long
.
If int
has a width of 16 bits, then 0xFFFF
is larger than the maximum value representable by an int
. Thus, 0xFFFF
is of type unsigned int
, which is guaranteed to be large enough to represent 0xFFFF
.
When the usual arithmetic conversions are performed for evaluation of the &
, the unsigned int
is converted to a long
. The conversion of a 16-bit unsigned int
to long
is well-defined because every value representable by a 16-bit unsigned int
is also representable by a 32-bit long
.
There's no sign extension needed because the initial type is not signed, and the result of using 0xFFFF
is the same as the result of using 0xFFFFL
.
Alternatively, if int
is wider than 16 bits, then 0xFFFF
is of type int
. It is a signed, but positive, number. In this case both operands are signed, and long
has the greater conversion rank, so the int
is again promoted to long
by the usual arithmetic conversions.
As others have said, you should avoid performing bitwise operations on signed operands because the numeric result is dependent upon how signedness is represented.
Aside from that, there's nothing particularly wrong with this code. I would argue that it's a style concern that value
is not initialized when it is declared, but that's probably a nit-pick level comment and depends upon the contents of the //some stuff
section that was omitted.
It's probably also preferable to use a fixed-width integer type (like uint32_t
) instead of long
for greater portability, but really that too depends on the code you are writing and what your basic assumptions are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With