Bluehost.com Web Hosting $6.95

Re: bit operation

This is a discussion on Re: bit operation within the Linux Networking forums, part of the Linux Forums category; In article <clarkcox3-28AF68.20294318022004@localhost>, Clark Cox <clarkcox3@mac.com> wrote: > In article <...


Go Back   Usenet Forums > Linux Forums > Linux Networking

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 02-19-2004
Barry Margolin
 
Posts: n/a
Default Re: bit operation

In article <clarkcox3-28AF68.20294318022004@localhost>,
Clark Cox <clarkcox3@mac.com> wrote:

> In article <9ut01c.d1q.ln@news.it.uc3m.es>, ptb@notme.com (P.T.B)
> wrote:
>
> > In comp.os.linux.networking Martin Dickopp
> > <expires-2004-03-31@zero-based.org> wrote:
> > > Followup-To: comp.lang.c since this part of the thread discusses the C
> > > language.

> >
> > > ptb@oboe.it.uc3m.es (P.T. Breuer) writes:
> > > > In comp.os.linux.networking Marcia Hon <honm@rogers.com> wrote:
> > > >> The reason I was having that problem is that char wraps around itself
> > > >> beyond -126 to 127. So, the bits were wrapped around.
> > > >
> > > > No, this is not so. char is signed.
> > >
> > > No, `char' can be either a signed or an unsigned type.

> >
> > Oh, shut up! I said IS, not "can be".

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


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, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Reply With Quote
  #12 (permalink)  
Old 02-19-2004
P.T. Breuer
 
Posts: n/a
Default Re: bit operation

In comp.os.linux.networking Clark Cox <clarkcox3@mac.com> wrote:
> In article <9ut01c.d1q.ln@news.it.uc3m.es>, ptb@notme.com (P.T.B)
> wrote:
>
> > In comp.os.linux.networking Martin Dickopp
> > <expires-2004-03-31@zero-based.org> wrote:
> > > Followup-To: comp.lang.c since this part of the thread discusses the C
> > > language.

> >
> > > ptb@oboe.it.uc3m.es (P.T. Breuer) writes:
> > > > In comp.os.linux.networking Marcia Hon <honm@rogers.com> wrote:
> > > >> The reason I was having that problem is that char wraps around itself
> > > >> beyond -126 to 127. So, the bits were wrapped around.
> > > >
> > > > No, this is not so. char is signed.
> > >
> > > No, `char' can be either a signed or an unsigned type.

> >
> > Oh, shut up! I said IS, not "can be".

>
> You said 'char is signed', not 'char is signed on my platform', there


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!


> is a difference.


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.

> > The cast is "superfluous" in that it will be promoted to int anyway,
> > but that is why I added the explicit cast!

>
> You added an explicit cast to make sure that something that happens
> automatically would happen? That doesn't make much sense.


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!

> > > (Except on systems where `char'
> > > is unsigned and `sizeof(unsigned int)' is equal to 1,

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

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


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


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

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


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.


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

> >
> > That doesn't matter - it's the same size,

>
> It doesn't matter if it's the same size or not, it's undefined
> behavior.


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.

> > 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.
> >
> > > > 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.
> > >
> > > It will be promoted to `int', not necessarily a 32-bit integer,

> >
> > YES, "necessarily" a 32 bit integer, since that is what it IS.

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


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
Reply With Quote
  #13 (permalink)  
Old 02-19-2004
pete
 
Posts: n/a
Default Re: bit operation

P.T. Breuer wrote:
> In comp.os.linux.networking Clark Cox <clarkcox3@mac.com> wrote:
> > In article <9ut01c.d1q.ln@news.it.uc3m.es>,
> > ptb@notme.com (P.T.B) wrote:


> > > If char is unsigned then it will be promoted to
> > > (unsigned) int FULL STOP, never mind how big an int is.

> >
> > Not true, take the following implementation for instance:


> You are saying that that the char 255 will be promoted not to int
> but to an unsigned int of value 255?


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.

> Which of course
> is -1 when read as an int (since ints are 8 bit in your scheme)?


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.

>
> Pinch me. Tell me the difference.
>
> > > > An `int' argument is an error here,
> > > > since `%x' expects an `unsigned int' argument.
> > >
> > > That doesn't matter - it's the same size,

> >
> > It doesn't matter if it's the same size or not, it's undefined
> > behavior.

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


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
Reply With Quote
  #14 (permalink)  
Old 02-19-2004
Old Wolf
 
Posts: n/a
Default Re: bit operation

> > ptb@oboe.it.uc3m.es (P.T. Breuer) writes:
> > > In comp.os.linux.networking Marcia Hon <honm@rogers.com> wrote:
> > >> The reason I was having that problem is that char wraps around itself
> > >> beyond -126 to 127. So, the bits were wrapped around.
> > >
> > > No, this is not so. char is signed.

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

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


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

>
> > > I imagine you did
> > >
> > > printf("%x", (int)mychar);

> >
> > If `mychar' has type `char', the cast is superfluous,

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


To be consistent, you should have written:
(int (*)(const char *, ...))printf((const char *)"%x", (int)mychar);

> > (Except on systems where `char'
> > is unsigned and `sizeof(unsigned int)' is equal to 1,

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


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

> > where it is
> > promoted to `unsigned int'.)

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


Actually it gets promoted to signed int (except for implementations
where this is not always possible -- namely, those with
sizeof(int) == 1)
Reply With Quote
  #15 (permalink)  
Old 02-19-2004
P.T. Breuer
 
Posts: n/a
Default Re: bit operation

In comp.os.linux.networking Old Wolf <oldwolf@inspire.net.nz> wrote:
> > > ptb@oboe.it.uc3m.es (P.T. Breuer) writes:
> > > > In comp.os.linux.networking Marcia Hon <honm@rogers.com> wrote:
> > > >> The reason I was having that problem is that char wraps around itself
> > > >> beyond -126 to 127. So, the bits were wrapped around.
> > > >
> > > > No, this is not so. char is signed.
> > >
> > > No, `char' can be either a signed or an unsigned type.

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

>
> When posting in comp.lang.c you should talk about generic


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

> implementations. If not, then don't post here.


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!

> Your statement "char is signed." looks much more like


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

> "char is always signed" (a common misconception) than


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.

> "char is signed on your platform", to me anyway
> (and probably to the OP).


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.


> > > > I imagine you did
> > > >
> > > > printf("%x", (int)mychar);
> > >
> > > If `mychar' has type `char', the cast is superfluous,

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

>
> To be consistent, you should have written:
> (int (*)(const char *, ...))printf((const char *)"%x", (int)mychar);



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.

> > > (Except on systems where `char'
> > > is unsigned and `sizeof(unsigned int)' is equal to 1,

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

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


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.

> Also, systems with sizeof(unsigned int) == 1 are not that
> uncommon,


Oh yes they are.

> > > where it is
> > > promoted to `unsigned int'.)

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

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


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
Reply With Quote
  #16 (permalink)  
Old 02-20-2004
CBFalconer
 
Posts: n/a
Default Re: bit operation

"P.T. Breuer" wrote:
> In comp.os.linux.networking Old Wolf <oldwolf@inspire.net.nz> wrote:
>

.... snip ...
>
> 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.
>
> > "char is signed on your platform", to me anyway
> > (and probably to the OP).

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

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


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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:09 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0