Question

I do not fully understand the difference between a bitwise operation and a logical operation. Please see the bitwise operation below:

x=7 and 15

I understand that x will equal 7 in this case after inspecting each bit individually.

How does boolean logic work at a lower level. I believe that a Boolean is a 32 Bit data type (I could be wrong). Are boolean literals (TRUE and FALSE) treated as single bits?

Was it helpful?

Solution 2

This is generally how these things work.

Bitwise operations perform Boolean operations on the bits representing their operands. Logical operations are Boolean operations performed on Boolean sets, usually TRUE and FALSE or 1 and 0. A Boolean set could be any group of distinct items where the total count of set members is 2.

In the case of performing bitwise AND on the operands 7 and 15: if the system you're using represents them as straight binary numbers with no weird offsets then 7 becomes 0111 in binary, 15 becomes 1111 in binary. The leading 0 on binary 7 is not necessary as it is the same as prepending a zero to any decimal number: 10 = 010 = 000000000000010, it's easier to illustrate the operation with the leading zero though.

0111   7 in binary
1111  15 in binary
&&&&  bitwise AND
0111  results in 7

if true and false are represented as binary 1 and 0 then there is no difference between the "logical" and and the "bitwise" and operations.

1   true as binary 1
0   false as binary 0
&   either logical or bitwise AND
0   results in binary 0

try bitwise and on some other numbers like say 1 and 2

01 1 in binary
10 2 in binary
&& bitwise and
00 results in binary 0

Assuming that any number except 0 will be converted to true, performing a "logical" and on the numbers 1 and 2 would yield a different result

1 the number 1 converted to true and represented as boolean 1
1 the number 2 converted to true and represented as boolean 1
& logical and
1 results in true, here represented as binary 1

When performing bitwise operations involving floats, negative numbers, or basically anything other than positive whole numbers, you may end up with radically different results. This is due to different environments using different methods for storing numbers in memory. For sure, it's all binary but, the number line isn't always centered on binary zero and it's not always the case that every bit represents part of the number; some of the bits could represent the sign of the number or the exponent. You'll have to dig down into the implementation details if you want to just toss decimal numbers into bitwise operations. It's easier to deal with if you have a method of converting the numbers to binary because then you know the bits and their order exactly and, you'll be able to test whether your expectations match your results.

OTHER TIPS

Do not make any assumption about the internal structure of a Boolean. In many languages (i.e. C, C++), bool is internally represented as int and everything except the value 0 is interpreted as true. But this is impmentation-dependent and may change between compilers or even compiler versions. In modern languages such as java or c#, (the equivalent of) the above statement will not even compile, because an int cannot be implicitly cast to a bool.

If you really need bitwise operation, the statement is of course valid, the value of x will be 7 (as this is the bitwise and of 7 and 15), but to convert this to Boolean, it would be wise to do it explicitly, i.e if (x != 0) then... or bool xb = x != 0. This might help avoiding confusion to the reader of your code and also makes clear that you're aware of the fact that you're now doing a type conversion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top