Velocity Reviews > tilde

# tilde

vippstar@gmail.com
Guest
Posts: n/a

 09-15-2008
On Sep 15, 11:44 pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> 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.
>
> unsigned int x;
> x=5;
> x=~15;
> printf("%i\n",x);

What does K&R tell you about %i?

<snip>

Keith Thompson
Guest
Posts: n/a

 09-15-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> 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.

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.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

vippstar@gmail.com
Guest
Posts: n/a

 09-15-2008
On Sep 16, 12:33 am, "Bill Cunningham" <(E-Mail Removed)> wrote:
> Correction. I tried using an int and received the integer values of 5
> and -16. Maybe this is right. And not having ever used ui's because my
> machine sees i's and ui's both as 4 bytes. I use the wrong printf format
> conversion. When I use unisgned int I got
> 5i
> and 16i
>
> when using the correct %ui and unsigned int x; That's weird.

Where does K&R2 mention %ui?

Bill Cunningham
Guest
Posts: n/a

 09-15-2008
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.

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.

I looked at a hexdump of an x86 mbr. Later in the dump was whole bytes
turned off. I guess it's some kind of padding in the code. So if I take the
first 8 bits and print them out in decimal I can deduce the endianness of
the entire dump.

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.

Bill

Bill Cunningham
Guest
Posts: n/a

 09-15-2008
Correction. I tried using an int and received the integer values of 5
and -16. Maybe this is right. And not having ever used ui's because my
machine sees i's and ui's both as 4 bytes. I use the wrong printf format
conversion. When I use unisgned int I got
5i
and 16i

when using the correct %ui and unsigned int x; That's weird.

Bill

Keith Thompson
Guest
Posts: n/a

 09-15-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> Correction.

Correction to what? Context, please.

> Correction. I tried using an int and received the integer values of 5
> and -16. Maybe this is right. And not having ever used ui's because my
> machine sees i's and ui's both as 4 bytes. I use the wrong printf format
> conversion. When I use unisgned int I got
> 5i
> and 16i
>
> when using the correct %ui and unsigned int x; That's weird.

This is very nearly meaningless without seeing actual code.

"%ui" is not a correct format. Consult K&R to find out the correct
form.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham
Guest
Posts: n/a

 09-15-2008

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> This is very nearly meaningless without seeing actual code.
>
> "%ui" is not a correct format. Consult K&R to find out the correct
> form.

Ok I see %u now.

Bill

osmium
Guest
Posts: n/a

 09-16-2008
"Bill Cunningham" writes:

> 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.

The answer is on page 49 of K&r. It says "The unary operator ~ yields
.....". Do you know what unary means? Did you try to find out? What are the
consequences of this property in the code above? In K&R every word counts,
ignore words at your own peril. From what little I have seen of your posts
you would be much better off with another, more wordy book. K&R is written
for people who can and will FOCUS.

The first book in this list is the first one I would think of recommending.

http://users.powernet.co.uk/eton/clc/cbooks.html