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

- 06-10-2005tinkerbell20Bitwise OR
What's wrong with this:

Code:`AndIt = Test | 4.563;`

- 06-10-2005jverkoey
You can't BOR floating point values, only integral numbers.

- 06-10-2005DougDbugQuote:

Is it wrong because there's a float number?

**Your guess is correct.**You can't (normally) use bitwise operators on floats.

There's a good introduction to bitwise operators in the Programming FAQ.

Quote:

does it compare the last digits or all? Thanks...

**It compares all of the**. If you are comparing 32 bit integers, all 32 bits will be compared.*bits*

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... - 06-11-2005xErathQuote:

Originally Posted by**tinkerbell20**

- 06-11-20057stud
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:

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:

Code:`1`

8's 4's 2's 1's

Code:`1 0`

8's 4's 2's 1's

Code:`1 0 1`

8's 4's 2's 1's

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:

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.