tilde

V

vippstar

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

Keith Thompson

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

vippstar

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?
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
K

Keith Thompson

Bill Cunningham said:
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.
 
B

Bill Cunningham

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
 
O

osmium

Bill Cunningham said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top