1. bitwise operators

Hello,

I have read chapter 2.8.2.3 several times and I still don't get it.

I know that and is that both must be true.
Is that with the bitwise and also ?

and what means XOR, right and left shift and one compliments.

I have looked at the script but it don't make it more clear.

Roelof

2. Originally Posted by roelof
Hello,

I have read chapter 2.8.2.3 several times and I still don't get it.
Oh my... just wait till chapter 7 ... Just kidding. Bitwise operations are probably the most confusing part of any programming language. Worse than pointers, in my opinion...

The first thing you need is "truth tables" for the various functions... and yes, these are largely derived from logic gates AND OR NOT and XOR... so they're actually pretty easy once you get the concept.

For simplicity lets envision a logic "blob" with 2 inputs A and B and 1 output...

For an AND situation A and B both have to be TRUE (1) before the output is TRUE ... if output is FALSE if either A or B or both are FALSE.
For an OR situation if either or both of A or B is TRUE the output is TRUE... for the output to be FALSE both A and B must be FALSE.
For an XOR situation if either A or B is true the output is TRUE... the output will be false if A and B are both FALSE or A and B are both TRUE.

Now lets picture 8 of these things stacked up beside each other being fed by bytes from the computer...

Code:
```A =       1 0 1 1 1 0 0 1
B =       1 1 1 0 1 1 0 0

AND =     1 0 1 0 1 0 0 0
OR =      1 1 1 1 1 1 0 1
XOR =     0 1 0 1 0 1 0 1```
This is based on ANDing, ORing or XORing, the individual bits in the bytes. (Of course it works on any integer type variable)

The most common use is in bit filtering to apply various flags or eliminate unwanted elements from a variable. For example:
Code:
```// is a number odd?
int b = A & 1;  // b = 0 or 1, lowest bit is always 1 for an odd number

// make a number odd
int b = A | 1;  // set the lowest bit on.

// flip even and odd
int b = A ^ 1; // toggle the lowest bit```
Bit shifting is a bit strange but far simpler... Here you need to picture a clear plastic tube with 7 white balls and one red one... If you put a new white ball in the left end you push everything to the right, effectively moving the red ball over one space... Same from the other end, except now it moves one position left...

For example...
Code:
```// merge two bytes in a short int.
char B = 0xC0DE;

short C = A << 8;  //move A over 8 positions left and assign it to C
C += B;        // add the value of B to C.

Does that help?

3. Yes. "AND" means both must be true.

if (condition AND condition) ...

Same with bits. 1 AND 1 ---> 1

XOR means one or the other exclusively (not both)

4. Oke,

Everything thanks.
And pointers are not chapter 7 but chapter 5 and I hope I will understand that .
It also looks very confusing but that is a problem when I get there.

Roelof

5. Sorry I still don't get it.

I have this question :

e.How would you turn off all but the low-order four bits in i1
and I1 is a integer.

The answer in the book says : i1 & = oxf

Oke , lets say i1 = 1 binair 00000001

Then it would be :

00000001 (1)
00001111 (f)
---------
00000001 (outcome)

Then the outcome of and & would be 00000001
So the last four bits are not turned off.

Im a right or do i make a big mistake.

Roelof

6. Your book is wrong (perhaps it's why you're having so much trouble)

To clear the bottom 4 bits, it would be
Code:
```i1 &= ~ 0xf;

00000001 (1)
11110000 (~ 0xf)
---------
00000000 (outcome)```
~ is bit-wise not (flip all the bits).
So regardless of the size of your integer (2, 4, 8 bytes), the top n-4 bits will all be 1's, and the bottom 4 will be zero.

7. Hello,

Maybe find a better online book in the book recommendations.
But thanks for the help.

Roelof

8. Hello,

What do you think about C programming - A modern approach 2nd Edition for a beginner programmer ?

Roelof

9. Originally Posted by roelof
Sorry I still don't get it.

I have this question :

e.How would you turn off all but the low-order four bits in i1
and I1 is a integer.

The answer in the book says : i1 & = oxf

Oke , lets say i1 = 1 binair 00000001

Then it would be :

00000001 (1)
00001111 (f)
---------
00000001 (outcome)

Then the outcome of and & would be 00000001
So the last four bits are not turned off.

Im a right or do i make a big mistake.

Roelof
Read the test question again... it says turn off all but the low order 4 bits... that is eliminate bits 5 through n... The book answers with i1 & 0xF which is correct.

Try working the problem with higher values....
Code:
```1100 0101
0000 1111
----------
0000 0101```
Get it now?

10. Originally Posted by roelof
Hello,

Maybe find a better online book in the book recommendations.
But thanks for the help.

Roelof
Or you could just read more carefully

11. Oke,

I get it but still I try now the other book.
But still many many thanks.

Roelof

12. No worries, my friend... Good luck with the new book...

13. Except that the book wasn't wrong to begin with.

14. Originally Posted by nonoob
Except that the book wasn't wrong to begin with.
Maybe so... but I got the impression Roelof wasn't too happy with the way it was teaching either...
If you think about it hitting someone with Bitwise operations in chapter 2 is kinda nasty.