Bill Cunningham said:
I was told when usin bitwise operators to unsigned types. Why does p 48
2.9 of kandr2 say that these operators work with both signed and unsidgned
chars, ints, and so on. I tried this peice of code and hope that it had the
effect of "flipping" the bits.
There's no contradiction.
Bitwise operators are generally more useful, and make more sense, when
applied to unsigned types. They're legal for signed types, but it
rarely makes much sense to use them that way; you have to worry about
what's going to happen to the sign bit.
To put it another way, an unsigned type can be used either as a number
(using operators like +, -, *, /), or as a sequence of bits (using the
bitwise operators). Signed types can also, but if you want to deal
with bit sequences, unsigned types just work better.
unsigned int x;
x=5;
x=~15;
printf("%i\n",x);
The binary printed -11. Endianness can be discovered by this I see. I've
always had that problem in the past of not knowing the endianness of bits.
99% of the time you don't need to know or care about endianness.
[...]
Question: I see that the bitwise operators are usually written like
this.
x=x|5;
I received a compiler complaint when I tired
x=x~15 and had to use x=~15 why is that. If this question sounds dumb it's
becuase I am dumb to these things.
Have you studied these operators in K&R2? Your question leads me to
suspect that you haven't. What does "~" mean? What does "|" mean?
(Don't tell us the answer, look it up and find out.)