tilde

Discussion in 'C Programming' started by vippstar@gmail.com, Sep 15, 2008.

  1. Guest

    On Sep 15, 11:44 pm, "Bill Cunningham" <> 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>
     
    , Sep 15, 2008
    #1
    1. Advertising

  2. "Bill Cunningham" <> 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.


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

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Sep 15, 2008
    #2
    1. Advertising

  3. Guest

    On Sep 16, 12:33 am, "Bill Cunningham" <> 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?
     
    , Sep 15, 2008
    #3
  4. 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, Sep 15, 2008
    #4
  5. 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
     
    Bill Cunningham, Sep 15, 2008
    #5
  6. "Bill Cunningham" <> 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) <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"
     
    Keith Thompson, Sep 15, 2008
    #6
  7. "Keith Thompson" <> wrote in message
    news:...

    > 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
     
    Bill Cunningham, Sep 16, 2008
    #7
  8. osmium Guest

    "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
     
    osmium, Sep 16, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SStory

    Tilde for relative path

    SStory, Oct 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,762
    SStory
    Oct 19, 2003
  2. CHristian Niss
    Replies:
    0
    Views:
    486
    CHristian Niss
    Oct 31, 2003
  3. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    Use of Tilde (~) and <LINK>

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, May 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,483
    =?Utf-8?B?QWxleCBNYWdoZW4=?=
    May 11, 2004
  4. MB2

    Tilde Expansion

    MB2, Sep 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    450
    Rick Strahl [MVP]
    Sep 24, 2004
  5. =?Utf-8?B?U2hhd24=?=

    Problems with Tilde in Microsoft Development Model

    =?Utf-8?B?U2hhd24=?=, Dec 24, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    672
    =?Utf-8?B?U2hhd24=?=
    Jan 5, 2005
Loading...

Share This Page