1. Bitwise operators. What's this????????

I have maaaaaaaany difficulties in understanding the Bitwise Operators. In fact i understand nothing

Here are my questions on some statements:
Code:
`unsiged c, displayMask = 1 << 31;`
what the bold means, and what's the value of displayMask?

Code:
`putchar( value & displayMask ? '1' : '0' );`
value is an unsigned integer.
1) What the result of the comparison? I mean, shouldn't be something like that:
value & displayMask == 1 ? .. : ...
?
2)What does the comparison do? How it compares the bits of the two operands, and where does it know the bits of the unsigned "value"?

Code:
`value <<= 1;`
What does this do??, what's the new value of "value"?

2. Shouldn't you be doing your own homework?

the << operator shifts N places to the left
the >> operator shifts N places to the right

Thus:

a << 3

shifts "a" three places to the left.

The rest you should easily be able to work out. All you need is a simple printf statement and you'll have all your questions answered.

Quzah.

This is not my homework. Is the lesson of my book.

>the << operator shifts N places to the left
>the >> operator shifts N places to the right
i don't understand you. What do you meen?

4. Probably the easiest way to think about this is multiplication and division by powers of 2.

Eg.

4 << 1 is equal to 4 * 2. (8)
4 << 2 is equal to 4 * 4 (16)
4 << 3 is equal to 4 * 8 (32)

and so on... Right shifting is the same, but using division instead of multiplication.

In computer terms however, this shifting is MUCH faster than any multiplcation / division commands.

To understand it fully however, you need to know how a byte is stored. I feel this is beyond the scope of this forum.

About the comparison: Anything resolving to zero always gives false. Anything non-zero would always give true, so there's no need for the "== 1" in your comparison.

5. Ok, but...

Ahaaaa!!!! Thanks a lot Elixia!!!!! But i still have some BIG questions:

1) unsiged displayMask = 1 << 31;
This has the value 0. Why does he initialize displayMask in that way?

2) value <<= 1;
Meens: value = value << 1 . Which means, that if value was 20, it is now 20*2 = 40. Right?

3) value & displayMask ( == 1 )
how does & compare the 2 operands? What does it do? I am very confused at this point.. ( Can you give me an example with numbers to understant it please? )

6. I don't feel that discussion of how a byte is stored is beyond the scope of this forum. It's important to know, and it isn't that hard.

There are 8 bits in a byte. 4 High order bits (bigger ones...like the thousand place in base 10 compared to the ones place) and 4 low order bits (like the ones place)

0000 0000

That number, converted to base 10, is 0.

0000 1111 converted to hex is FF - converted to base 10 it's 255.

Each place in a binary number has different significance

{128} {64} {32} {16} {8} {4} {2} {1}

0 1 0 0 0 0 0 1

So, the value of that number is 1(64)+1(1) = 65
If you shifted all the bits one to the left, you would get
1000 0010, the value of which is 1(128)+1(2)=130

Hope that helps. If not, look up a tutorial on binary or something.

7. & means 'and.'

Here's the table for and

Code:
```    0   1
0   0   0
1   0   1```
If both values are set for any given bit, that bit in the result will be set. Otherwise, that bit in the result will be 0.

8. Money?
What book are you getting these bitwise examples from? Most good C books will explain this topic at a beginner level.

9. >I don't feel that discussion of how a byte is stored is beyond the scope of this forum.
I agree, but a thorough discussion tends to be too long for any but the most interested to read. Unless you're assuming something fixed like big-endian octets.

>There are 8 bits in a byte.
No, there are 8 bits in an octet. The number of bits in a byte is not a fixed number and can change depending on the machine you use.

>This has the value 0. Why does he initialize displayMask in that way?
Bitwise operations are inherently nonportable because it requires assumptions about the binary representation of data. The person who wrote that line of code was assuming 4 byte integers, 1 << 31 being the equivalent of INT_MAX, or 2147483648. Most likely, you're running your test on a machine that uses 2 byte integers and the value overflows to 0.

>Meens: value = value << 1 . Which means, that if value was 20, it is now 20*2 = 40. Right?
Yes.

>how does & compare the 2 operands?
& compares the two operands bit by bit and returns a value with each bit set where both operands had a bit set:

00010101 == 21
00101101 == 45

Those two values ANDed together would result in

00000101 == 5

because only the first and third bits (from the right side) are set to 1 in both operands. & means to set a bit in the result only if the same bit in both operands is set.

>Most good C books will explain this topic at a beginner level.
Most good C books also don't elaborate on what use this topic is good for. I've found that this is where the confusion lies.

10. Ok, thanks... I think i understood. But i am only at the beginning of these operators and i don't see the future very well...

11. Originally posted by Prelude
>
Most good C books also don't elaborate on what use this topic is good for. I've found that this is where the confusion lies.
I agree, I had tons of trouble figuring out what and how to use bitwise operators. My book only mentions them in the appendix and provides zero explanation. Then I tryed reading K&R's explanation but it still wasn't good enough for me, I didn't understand them.

money try reading this, it explains it very well and shows how you can use them to your benefit in a program.
http://www.gamedev.net/reference/art...rticle1563.asp

12. Here's a nice example for you. C only supports numbers from 8-bits upwards. There's no direct support for numbers less than 8 bits.

Now, lets assume your memory usage needs every byte accounted for as you have any got a few hundred of them for your entire program.

You want two counters, but using 2 chars would be a waste, so you can do something like this:

Code:
```   char counters = 0;

for (;(counters & 0x0F) < 10; counters ++)
{
for (;((counters & 0xF0) >> 4) < 10; counters += 0x10)
{
printf ("First counter: %d Second counter: %d", counters&0x0F, (counters & 0xF0) >> 4);
}
counters &= 0x0F;
}```
Have a play with that and see how it works.

13. Here is a fine article on bits and bitwise operators.

Quzah.

14. It's a bit difficult to understand your code...

char counters = 0;
How do you initialize it? 0 is an integer

0x0F?
What's this? Does the computer understand it?

Can you explain me how it work?

15. are you reading C How To Program by Deitel and Deitel? i had the same confusion when i first read the chapter, but you just have to reread it and reread it carefully. It's not an easy topic, but once you get it down, it's really not that hard.