Professional Web Applications Themes

bit operation - UNIX Programming

Hi, The reason I was having that problem is that char wraps around itself beyond -126 to 127. So, the bits were wrapped around. thanks, Marcia...

  1. #1

    Default Re: bit operation

    Hi,

    The reason I was having that problem is that char wraps around itself
    beyond -126 to 127. So, the bits were wrapped around.

    thanks,
    Marcia


    Marcia Guest

  2. #2

    Default [OT] Re: bit operation


    On Mon, 16 Feb 2004, Marcia Hon wrote in comp.lang.c: 

    You have [probably serendipitously] hit upon an excellent way to
    get a whole bunch of people annoyed at you: Post a random and almost
    certainly wrong observation, with no context, to three different
    Usenet newsgroups.
    Don't do this. If you have a question, post it to the most
    appropriate group. If you have an observation, post a *complete*
    thought (including antecedents for all your "this"es and "that"s)
    to the most appropriate group. But don't assume that people in the
    Big World Outside Your Room [tm] have an instantaneous grasp of
    whatever it was you were thinking when you typed your message. We
    don't know [and frankly I don't care] what "that problem" was to
    which you refer.

    However, I *do* know that the values of 'char' in the C programming
    language *do not* and *cannot* wrap from -126 to 127. So either
    you're mistaken, or you are programming in some really funky language
    that's not topical in this newsgroup.

    HTH,
    -Arthur

    Arthur Guest

  3. #3

    Default Re: bit operation

    In article <_FYXb.72$bloor.is.net.cable.rogers.com>,
    "Marcia Hon" <com> wrote:
     

    First, what problem were you having?
     

    No, the char type in C is not allowed to do that. If the char type
    can represent any negative values at all (i.e. it is not unsigned) then
    it must be able to represent the number -127, so it cannot wrap at -126,
    if it does so, then your compiler is broken.
     

    Whatever that means.

    Also, whatever your problem was, I find it hard to believe that it could
    have been relevant to comp.lang.c, comp.os.linux.networking *and*
    comp.unix.programmer.

    Clark Guest

  4. #4

    Default Re: bit operation

    In comp.os.linux.networking Marcia Hon <com> wrote: 

    No, this is not so. char is signed. I imagine you did

    printf("%x", (int)mychar);

    which will do a signed extension of mychar to an int. Since mychar =
    141 has the sign bit set (it is -115, I suppose, or 0x8d), it will be
    turned into a 32-bit integer by repeating the sign bit to the left in
    the extension, giving you 0xffffff8d.

    Peter
    P.T. Guest

  5. #5

    Default Re: bit operation

    Followup-To: comp.lang.c since this part of the thread discusses the
    C language.

    it.uc3m.es (P.T. Breuer) writes: 
    >
    > No, this is not so. char is signed.[/ref]

    No, `char' can be either a signed or an unsigned type.
     

    If `mychar' has type `char', the cast is superfluous, since a `char'
    argument is promoted to `int' anyway. (Except on systems where `char'
    is unsigned and `sizeof(unsigned int)' is equal to 1, where it is
    promoted to `unsigned int'.)

    An `int' argument is an error here, since `%x' expects an `unsigned int'
    argument.
     

    It will be promoted to `int', not necessarily a 32-bit integer, unless
    `int' happens to be a 32-bit integer on the platform in question.

    Martin
    Martin Guest

  6. #6

    Default Re: bit operation

    Marcia Hon wrote: 

    Another pointless new thread foolishly cross-posted. The time has
    come to PLONK. So even if you learn to post sanely, you will not
    be seen here.

    --
    Chuck F (com) (att.net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!


    CBFalconer Guest

  7. #7

    Default Re: bit operation

    In article <clarkcox3-CD9761.02063417022004localhost>,
    Clark Cox <com> wrote:
     
    >
    > First, what problem were you having?[/ref]

    I think he's referring to the problem he described in a different
    thread, titled "Bit operation not working". It was only cross-posted to
    comp.os.linux.networking (I'm guessing because his bit-twiddling is part
    of implementing a network protocol) and comp.unix.programmer, so if
    you're seeing this pseudo-followup in comp.lang.c it's understandable
    that you wouldn't know what the heck he's talking about.

    To the OP -- if you're following up on a posting, use your newsreader's
    Reply/Followup command, don't start a new thread.

    --
    Barry Margolin, mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    Barry Guest

  8. #8

    Default Re: bit operation

    In comp.os.linux.networking Martin opp <org> wrote: 

    Oh, jump in lake.
     
    > >
    > > No, this is not so. char is signed.[/ref]
    >
    > No, `char' can be either a signed or an unsigned type.[/ref]

    Oh, shut up! I said IS, not "can be". I know perfectly well what it
    can and can not be, and it IS signed, which is why I said it. When you
    learn to distinguish between concrete and generic instantiation, I will
    be happy. That IS a bus stop, OK? Yes, I know, it CAN BE a taxi stand
    also, but it IS a bus stop. OK? Now just retreat, desist, stop, go
    away.
     
    >
    > If `mychar' has type `char', the cast is superfluous,[/ref]

    The cast is "superfluous" in that it will be promoted to int anyway,
    but that is why I added the explicit cast! In order to make it plain
    what it was (which is what I said it was). Why else would I say what I
    said? Plese stop this. I didn't believe in idiots this big before now.
     

    Oh, hooo hooo. We are quick, aren't we?
     

    Yeah, I suppose like wow you really know how to program those ancient
    smart cards with the 256 word address space.
     

    Out of a mild but morbid interest, how the heck do you calculate that?
    If char is unsigned then it will be promoted to (unsigned) int FULL
    STOP, never mind how big an int is.
     

    That doesn't matter - it's the same size, and is not an "error" (yes, I
    do know the difference between a compiler advisory and an "error").
    Please come up to speed with the rest of the class, Einstein.
     
    >
    > It will be promoted to `int', not necessarily a 32-bit integer,[/ref]

    YES, "necessarily" a 32 bit integer, since that is what it IS. Get it?
    It happened - we are talking about the universe, real life, your tea
    pot. Learn to DISTINGUISH, fer crissakes, before you get murdered by
    a passing midget with a filed skateboard.
     

    The platform in question does have 32 bit integers. We know that.
    Why do we know that? Put your hand up. Yes ... OK, you at the back!
    yes, that's right, yes, .. because "0xffffff8d" was the value PRINTED.
     

    No. You mean "nit ram". It's bigendian. kindly get it right, and then
    crawl back deep underground. Come back when you mutter things that are
    not complete piffle that demonstrate only that you are unaware of what
    is being talked about. Look at what was said, do not imagine it. Yes, I
    am aware that you think you are giving a wonderful lesson on the
    possible implementations of C, and you are wrong. You are instead
    demonstrating that you cannot observe the data provided and deduce the
    way things ARE.


    P
    P.T.B Guest

  9. #9

    Default Re: bit operation

    In article <it.uc3m.es>, com (P.T.B)
    wrote:
     

    > >
    > > No, `char' can be either a signed or an unsigned type.[/ref]
    >
    > Oh, shut up! I said IS, not "can be".[/ref]

    You said 'char is signed', not 'char is signed on my platform', there
    is a difference.

    [snip]
     
    > >
    > > If `mychar' has type `char', the cast is superfluous,[/ref]
    >
    > The cast is "superfluous" in that it will be promoted to int anyway,
    > but that is why I added the explicit cast![/ref]

    You added an explicit cast to make sure that something that happens
    automatically would happen? That doesn't make much sense.
     
    >
    > Yeah, I suppose like wow you really know how to program those ancient
    > smart cards with the 256 word address space.[/ref]

    What does a 256 word address space have to do with sizeof(unsigned
    int) equaling 1?

     
    >
    > Out of a mild but morbid interest, how the heck do you calculate that?
    > If char is unsigned then it will be promoted to (unsigned) int FULL
    > STOP, never mind how big an int is.[/ref]

    Not true, take the following implementation for instance:

    char is an unsigned type
    sizeof(int) == 4
    sizeof(unsigned) == 4

    char will be promoted to int, as int is capable of representing the
    entire range of char, however, if sizeof(int) == 1, int will not be
    sufficent to represent the entire range, and char will therefore be
    promoted to unsigned int.
     
    >
    > That doesn't matter - it's the same size,[/ref]

    It doesn't matter if it's the same size or not, it's undefined
    behavior.
     
    > >
    > > It will be promoted to `int', not necessarily a 32-bit integer,[/ref]
    >
    > YES, "necessarily" a 32 bit integer, since that is what it IS.[/ref]

    No, int can be as small as 16-bits, or as large as 'long int'.

    Clark Guest

  10. #10

    Default Re: bit operation

    "P.T.B" wrote: 
    >
    > Oh, jump in lake.

    > >
    > > No, `char' can be either a signed or an unsigned type.[/ref]
    >
    > Oh, shut up! I said IS, not "can be". I know perfectly well what it
    > can and can not be, and it IS signed, which is why I said it. When you
    > learn to distinguish between concrete and generic instantiation, I will
    > be happy. That IS a bus stop, OK? Yes, I know, it CAN BE a taxi stand
    > also, but it IS a bus stop. OK? Now just retreat, desist, stop, go
    > away.[/ref]

    char may be signed or unsigned. You have no way of knowing except
    for a particular installation. c.l.c does not deal with
    particular installations, it deals with all of them in a fully
    portable manner. Your appearance here is doubtless the result of
    the OPs foul cross-posting, so please retreat to where you came
    from and remove c.l.c from any replies. I have attempted to
    assist in that.

    --
    Chuck F (com) (att.net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!


    CBFalconer Guest

  11. #11

    Default Re: bit operation

    In article <clarkcox3-28AF68.20294318022004localhost>,
    Clark Cox <com> wrote:
     
    > > 
    > >
    > > Oh, shut up! I said IS, not "can be".[/ref]
    >
    > You said 'char is signed', not 'char is signed on my platform', there
    > is a difference.[/ref]

    Not on his platform, on the OP's platform, which explains the behavior
    that the OP saw.

    If someone asks why they got wet when they went outside, you might say
    "because it's raining". You wouldn't be claiming that it's raining
    everywhere, just in the place relevant to the question.

    Yes, he could have been more precise and said "char is signed on your
    system". But leaving out details that don't seem to be relevant in a
    particular context (and might confuse more than enlighten) is not the
    same as being wrong.

    --
    Barry Margolin, mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    Barry Guest

  12. #12

    Default Re: bit operation

    In comp.os.linux.networking Clark Cox <com> wrote: 
    > > 
    > >
    > > Oh, shut up! I said IS, not "can be".[/ref]
    >
    > You said 'char is signed', not 'char is signed on my platform', there[/ref]

    I saud char IS signed, and I am not talking about MY platform, but
    about THE platform that is the subject of this thread. Now GO AWAY!

     

    There is no difference, since (a) we are not talking about MY platform,
    (b) we are talking about A particular platform, one which you seem not
    to have anything relevant to contribute about. Now GO AWAY.
     
    >
    > You added an explicit cast to make sure that something that happens
    > automatically would happen? That doesn't make much sense.[/ref]

    NO you natwad, I added an explictit cast to make what the compiler does
    mplicitly EXPLICT!!!!!! Is it so hard for your noggin to absorb the
    concept of EXPLANATION, even though my post consisted of nothing BUT?
    GO AWAY!
     
    > >
    > > Yeah, I suppose like wow you really know how to program those ancient
    > > smart cards with the 256 word address space.[/ref]
    >
    > What does a 256 word address space have to do with sizeof(unsigned
    > int) equaling 1?[/ref]

    Perhaps you'll tell me the size of long on your platform, and then we'll
    know.

     
    >
    > Not true, take the following implementation for instance:
    >
    > char is an unsigned type
    > sizeof(int) == 4
    > sizeof(unsigned) == 4
    >
    > char will be promoted to int, as int is capable of representing the
    > entire range of char, however, if sizeof(int) == 1, int will not be
    > sufficent to represent the entire range, and char will therefore be
    > promoted to unsigned int.[/ref]

    You are saying that that the char 255 will be promoted not to int
    but to an unsigned int of value 255? Which of course
    is -1 when read as an int (since ints are 8 bit in your scheme)?

    Pinch me. Tell me the difference.

     
    > >
    > > That doesn't matter - it's the same size,[/ref]
    >
    > It doesn't matter if it's the same size or not, it's undefined
    > behavior.[/ref]

    It's perfectly well defined, since the receiving function is in in
    another compilation unit and doesn't know whether the (to it) remote
    compiler thinks the 4byte unit it just passed is signed or unsigned.
    And the (to us) local compiler doesn't know what the (to it) remote
    function expects, since it's a varargs type and interpretation is
    dynamic (and remote). It contracts only to put the stuff on the stack.
     
    > >
    > > YES, "necessarily" a 32 bit integer, since that is what it IS.[/ref]
    >
    > No, int can be as small as 16-bits, or as large as 'long int'.[/ref]

    Oh GO AWAY!!!!!! Int IS 32 bits, on THE PLATFORM BEING DISCUSSED! That
    is why we are talking in this funny way! Do you get it yet?

    Peter
    P.T. Guest

  13. #13

    Default Re: bit operation

    P.T. Breuer wrote: [/ref]
     
    > >
    > > Not true, take the following implementation for instance:[/ref][/ref]
     

    He's saying that what char gets promoted to, depends entirely
    on whether or not int can represent all of the values of char.
    If CHAR_MAX is greater than INT_MAX, then it can't, otherwise, it can.
     

    C allows for bytes which have more than 8 bits.
    CHAR_BIT in limits.h, tells how many bits are in a byte.
    There aren't any macros in the standard library which are identical
    on all systems.
     
    > >
    > > It doesn't matter if it's the same size or not, it's undefined
    > > behavior.[/ref]
    >
    > It's perfectly well defined, since the receiving function is in in
    > another compilation unit and doesn't know whether the (to it) remote
    > compiler thinks the 4byte unit it just passed is signed or unsigned.[/ref]

    The undefined behavior comes from the standard
    saying that it is undefined.
    It doesn't matter if you can't figure a way for the machine
    to get it wrong.
    The standard says that if you have the wrong type for the specifier,
    then you have undefined behavior.

    --
    pete
    pete Guest

  14. #14

    Default Re: bit operation

    > > it.uc3m.es (P.T. Breuer) writes: 
    > >
    > > No, `char' can be either a signed or an unsigned type.[/ref]
    >
    > Oh, shut up! I said IS, not "can be". I know perfectly well what it
    > can and can not be, and it IS signed, which is why I said it. When you
    > learn to distinguish between concrete and generic instantiation, I will
    > be happy.[/ref]

    When posting in comp.lang.c you should talk about generic
    implementations. If not, then don't post here.
    Your statement "char is signed." looks much more like
    "char is always signed" (a common misconception) than
    "char is signed on your platform", to me anyway
    (and probably to the OP).
     
    > >
    > > If `mychar' has type `char', the cast is superfluous,[/ref]
    >
    > The cast is "superfluous" in that it will be promoted to int anyway,
    > but that is why I added the explicit cast! In order to make it plain
    > what it was (which is what I said it was). Why else would I say what I
    > said? Plese stop this.[/ref]

    To be consistent, you should have written:
    (int (*)(const char *, ...))printf((const char *)"%x", (int)mychar);
     
    >
    > Yeah, I suppose like wow you really know how to program those ancient
    > smart cards with the 256 word address space.[/ref]

    256 word address space has nothing to do with the number of bits
    in an int, and even less to do with sizeof(unsigned int).
    FYI, ints (signed or unsigned) must have at least 16 bits.

    Also, systems with sizeof(unsigned int) == 1 are not that
    uncommon, for example some CPUs for embedded devices cannot
    work with anything other than 32 bits (so they have 32-bit chars).
     
    >
    > Out of a mild but morbid interest, how the heck do you calculate that?
    > If char is unsigned then it will be promoted to (unsigned) int FULL
    > STOP, never mind how big an int is.[/ref]

    Actually it gets promoted to signed int (except for implementations
    where this is not always possible -- namely, those with
    sizeof(int) == 1)
    Old Guest

  15. #15

    Default Re: bit operation

    In comp.os.linux.networking Old Wolf <net.nz> wrote: 
    > >
    > > Oh, shut up! I said IS, not "can be". I know perfectly well what it
    > > can and can not be, and it IS signed, which is why I said it. When you
    > > learn to distinguish between concrete and generic instantiation, I will
    > > be happy.[/ref]
    >
    > When posting in comp.lang.c you should talk about generic[/ref]

    I don't know where you are talking, nor do I care. But I wish you would
    stop.
     

    I am not talking about generic implementations, I am answering a
    question. The OP asked why something was so and offered a false
    explanation. I corrected the explanation, telling her what she is
    seeing. You can read it for yourself above in your own quote!
     

    It is not a statement. It is an explanation. Read it as such.
     

    I did not say that, and I am not responsible for your misconceptions,
    and I wish you would keep both them and your imaginative
    misinterpretations to yourself.
     

    The OP doesn't know a thing about "platforms", except possibly that they
    often break when walking rapidly. Kindly do not confuse it. If it knew
    what it were doing it would not offer such absurd non-theories as to
    what is going on. The explanation it wants IS that its char IS a signed
    quantity and that it IS promoted to a (signed) int when passed to the
    printf routine that prints it out, thus extending the sign bit left,
    effctively "filling out" the space to left with fffff. It did have some
    crackpot theory about chars really being full of fffs as far as I can
    make out.

    Note my capitalization of IS, and go take a deep cold long shower.

     
    > >
    > > The cast is "superfluous" in that it will be promoted to int anyway,
    > > but that is why I added the explicit cast! In order to make it plain
    > > what it was (which is what I said it was). Why else would I say what I
    > > said? Plese stop this.[/ref]
    >
    > To be consistent, you should have written:
    > (int (*)(const char *, ...))printf((const char *)"%x", (int)mychar);[/ref]


    To be consistent you should go shoot yourself. In what way is the type
    of the other elements here an issue? That's right, it isn't. Therefore
    "to be consistent" I should come over and break this ruler over your
    head until you stop behaving like a pratty schoolboy. Do I make myself
    clear?

    BTW, I maintain that the fact that the type of printf is a varargs
    means that the compiler is mandated to push the int on the stack and
    pay no attention to whatever is written in the format field for that arg
    - which, btw is not required to be a const in the fprintf instantiation
    of printf, so could not be examined anyway. As though even being a
    const permitted the compiler to figure it out, since the const can be
    passed in from another compilation unit.

    So all the nerrnerrs who are shouting further nonsenses about undefined
    behaviour can also go and shoot themselves, with thanks.
     
    > >
    > > Yeah, I suppose like wow you really know how to program those ancient
    > > smart cards with the 256 word address space.[/ref]
    >
    > 256 word address space has nothing to do with the number of bits
    > in an int, and even less to do with sizeof(unsigned int).
    > FYI, ints (signed or unsigned) must have at least 16 bits.[/ref]

    Sigh. I made that more precise in the next sentence, where I asked how
    big was his long. And if you ask me it is pretty short.
     

    Oh yes they are.
     
    > >
    > > Out of a mild but morbid interest, how the heck do you calculate that?
    > > If char is unsigned then it will be promoted to (unsigned) int FULL
    > > STOP, never mind how big an int is.[/ref]
    >
    > Actually it gets promoted to signed int (except for implementations
    > where this is not always possible -- namely, those with
    > sizeof(int) == 1)[/ref]

    And, as if I cared, exactly what difference does this make? The
    promotion happens because it is passed as an argument to a function
    call. Unless signed and unsigned ints are different sizes, the
    receiving function will care honaynanny about what the sending code
    thinks its sign is! So boo.


    Peter
    P.T. Guest

  16. #16

    Default Re: bit operation

    "P.T. Breuer" wrote: 
    .... snip ... 
    >
    > The OP doesn't know a thing about "platforms", except possibly that
    > they often break when walking rapidly. Kindly do not confuse it.
    > If it knew what it were doing it would not offer such absurd non-
    > theories as to what is going on. The explanation it wants IS that
    > its char IS a signed quantity and that it IS promoted to a (signed)
    > int when passed to the printf routine that prints it out, thus
    > extending the sign bit left, effctively "filling out" the space to
    > left with fffff. It did have some crackpot theory about chars
    > really being full of fffs as far as I can make out.
    >
    > Note my capitalization of IS, and go take a deep cold long shower.[/ref]
    .... snip ... 

    You appear to be a boorish juvenile unable to understand that
    c.l.c wants topical postings, and that you should restrict your
    non-topical material to groups where they are acceptable. The
    appropriate response is PLONK.

    --
    Chuck F (com) (att.net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!


    CBFalconer Guest

Similar Threads

  1. The operation has timed-out.
    By garikaps in forum ASP.NET Web Services
    Replies: 3
    Last Post: October 29th, 04:26 PM
  2. asking for your co-operation
    By attorrney attorney in forum Ruby
    Replies: 0
    Last Post: August 7th, 05:34 AM
  3. Operation Manual
    By Lonely in forum Photography
    Replies: 2
    Last Post: July 23rd, 11:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139