What's wrong with this:
Is it wrong because there's a float number? if it wasn't float, does it compare the last digits or all? Thanks... :pCode:AndIt = Test | 4.563;
Printable View
What's wrong with this:
Is it wrong because there's a float number? if it wasn't float, does it compare the last digits or all? Thanks... :pCode:AndIt = Test | 4.563;
You can't BOR floating point values, only integral numbers.
Your guess is correct. You can't (normally) use bitwise operators on floats.Quote:
Is it wrong because there's a float number?
There's a good introduction to bitwise operators in the Programming FAQ.
It compares all of the bits. If you are comparing 32 bit integers, all 32 bits will be compared.Quote:
does it compare the last digits or all? Thanks...
All of the bits are or'd individually like this little 4-bit example: 0011 | 1000 = 1011. That's NOT C++ code! It's not quite that easy to put binary in your program.
0011 = 3 decimal and 3 hex
1000 = 8 decimal and 8 hex
1011 = 11 decimal and B hex.
In order to understand that 3 | 8 = 11, you have to convert the numbers to binary. And, that's NOT addition... 3 | 9 = 11 too.
Normally, we use hex (hexadecimal) when we're doing bitwise operations, because its easy (for humans) to convert between hex and binary.
And, we're usually not thinking of the numbers as "numbers" (values)... it's usually thought-of as a "bit-pattern", where each bit represents the "state" of something... if an LED is on or off, if a switch is on or off, if the fan is running or not, etc...
floating point operations are done by the co-processor. floating point values are stored not in registers but in a floating point stack managed by the same co-processor. So bitwise operations aren't available to floats. Yet you can get the bit pattern from a float, store it in a integer and do the bitwise operations, although I think that's not what you want.Quote:
Originally Posted by tinkerbell20
Hi,
If you want to understand bitwise OR, you have to learn about "binary format" for numbers. Computers can only store numbers in binary format. In order to understand binary format, first let's take a look at the more common decimal format you are familiar with. Here is a number in decimal format:
146
The right most column is the 1's column, and there are six 1's in the number. The next column to the left is the 10's column and there are four 10's in the number. The next column to the left of that is the 100's column and there is one 100 in the number. There is a pattern: the right most column is 10^0 = 1, then next column to the left is 10^1 = 10, and the next column to the left of that is 10^2 = 100. The prefix "deci" is Latin for 10, hence the name "decimal" number system where the digits are powers of 10.
Binary format uses the binary number system. In the binary number system, if you have a number like this:
101
the right most column is the number of 1's, the next column to the left is the number of 2's, and the next column to the left is the number of 4's. So, the binary number:
is equal to 5. Just like with decimal numbers, there is a pattern: 2^0 = 1, and 2^1 = 2, and 2^2 = 4. The prefix "bi" means 2 in Latin, and in the binary number system the digits are powers of 2. Note that digits in a binary system are either 0 or 1, you can't have a 2 as a digit.Code:1 0 1
4's 2's 1's
So, how would you write the number 10 in binary format? First, find out the largest power of 2 that is less than 10. That would be 8, so you can do this:
Now, move one column to the right. How many 4's are there in 10 after subtracting the one 8 we have already accounted for? Zero, so you continue like this:Code:1
8's 4's 2's 1's
Move one column to the right again. How many 2's are there in 10 after accounting for the 8? There is one 2, so we can write:Code:1 0
8's 4's 2's 1's
Since we have accounted for an 8 and a 2, that adds up to 10, so there aren't any ones, and we can write:Code:1 0 1
8's 4's 2's 1's
So, 10 written in binary format is:Code:1 0 1 0
8's 4's 2's 1's
1010
Now, on to bitwise OR. Suppose we have two binary numbers:
1010
1100
(Note: the second binary number is equivalent to 12.)
If you want to binary OR those two numbers together, you examine each column of digits individually. The first column on the left has two 1's in it, the next column to the right has a 0 and a 1, etc. If you OR those two numbers together, the result is:
1010
1100 OR
------------
1110
The result is produced by looking at a column, and if either of the numbers is a 1, the result is a 1; and if both numbers are 0 then the result is 0. You write down the result directly below the column you are examining.
So, when you ask what's wrong with this binary OR:
my reply would be: what do you think you are doing in that statement?Code:AndIt = Test | 4.563;
Also, I think bitwise operators are an advanced topic, and I don't think author's should include it in a beginning C++ book. If they do, it should be in an appendix. Just so you know, nothing you learn about C++ from here on out will require that you know anything about bitwise operators, so you can safely skip the whole topic.