By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

What does this mean???

P: n/a
I got a small programm on net...but there was different initialisation
that I saw...

It was as follows:

for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...

Jun 19 '07 #1
Share this Question
Share on Google+
26 Replies


P: n/a
Shraddha wrote:
I got a small programm on net...but there was different initialisation
that I saw...

It was as follows:

for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...
Same as "i = -1;" I suppose.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 19 '07 #2

P: n/a
"Shraddha" writes:
>I got a small programm on net...but there was different initialisation
that I saw...

It was as follows:

for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...
Tilde is the one's complement operator. I would not think of that as
bulletproof code, the results might well be different on a one's complement
machine than they are on a two's complement machine. The result is
"negative zero" on a one's complement machine.
Jun 19 '07 #3

P: n/a
On Mon, 18 Jun 2007 20:28:36 -0400, Joe Wright
<jo********@comcast.netwrote:
>Shraddha wrote:
>I got a small programm on net...but there was different initialisation
that I saw...

It was as follows:

for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...
Same as "i = -1;" I suppose.
Not on a 1's complement or a signed magnitude machine.
Remove del for email
Jun 19 '07 #4

P: n/a
osmium said:

<snip>
Tilde is the one's complement operator.
Actually, it's the ones' complement operator, because it complements
many ones. "Two's complement", however, is correct.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 19 '07 #5

P: n/a
"Richard Heathfield" writes:
osmium said:

<snip>
>Tilde is the one's complement operator.

Actually, it's the ones' complement operator, because it complements
many ones. "Two's complement", however, is correct.
I don't know if that is some of that famous British humour or what. It is
the one's complement operator, it does *not* form a proper two's complement.
Jun 19 '07 #6

P: n/a
osmium said:
"Richard Heathfield" writes:
>osmium said:

<snip>
>>Tilde is the one's complement operator.

Actually, it's the ones' complement operator, because it complements
many ones. "Two's complement", however, is correct.

I don't know if that is some of that famous British humour or what.
Nope.
It is the one's complement operator,
No, it's the ones' complement operator. Observe the position of the
apostrophe. If you disagree, take it up with Knuth (TAOCP Vol 2,
Section 4.1).
it does *not* form a proper two's complement.
I didn't say it did. I was referring to your correct positioning of the
apostrophe in "two's complement", later in your article.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 19 '07 #7

P: n/a
"Richard Heathfield" writes:
>
osmium said:
>
>"Richard Heathfield" writes:
>>osmium said:

<snip>

Tilde is the one's complement operator.

Actually, it's the ones' complement operator, because it complements
many ones. "Two's complement", however, is correct.

I don't know if that is some of that famous British humour or what.

Nope.
>It is the one's complement operator,

No, it's the ones' complement operator. Observe the position of the
apostrophe. If you disagree, take it up with Knuth (TAOCP Vol 2,
Section 4.1).
>it does *not* form a proper two's complement.

I didn't say it did. I was referring to your correct positioning of the
apostrophe in "two's complement", later in your article.
Some of your alleged humour is on a par with your food. If I had not
responded an innocent passerby who didn't try to parse your response, as I
did, could be misled badly.
Jun 19 '07 #8

P: n/a
osmium said:
"Richard Heathfield" writes:
>>
osmium said:
>>
>>"Richard Heathfield" writes:

osmium said:

<snip>

Tilde is the one's complement operator.

Actually, it's the ones' complement operator, because it
complements many ones. "Two's complement", however, is correct.

I don't know if that is some of that famous British humour or what.

Nope.
>>It is the one's complement operator,

No, it's the ones' complement operator. Observe the position of the
apostrophe. If you disagree, take it up with Knuth (TAOCP Vol 2,
Section 4.1).
>>it does *not* form a proper two's complement.

I didn't say it did. I was referring to your correct positioning of
the apostrophe in "two's complement", later in your article.

Some of your alleged humour is on a par with your food.
I say again, there is no humour intended here. I am being perfectly
serious. I don't know what amusement *you* see in this discussion, but
I'm not seeing any at all.
If I had not
responded an innocent passerby who didn't try to parse your response,
as I did, could be misled badly.
I don't see how. You said "Tilde is the one's complement operator" and I
replied "No, it's the ones' complement operator". The correction seems
plain enough to me.

If you think my correction is spurious, I invite you to check the Knuth
reference I gave.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 19 '07 #9

P: n/a
"Richard Heathfield" wrote:
osmium said:
>"Richard Heathfield" writes:
>>>
osmium said:
>>>
"Richard Heathfield" writes:

osmium said:
>
<snip>
>
>Tilde is the one's complement operator.
>
Actually, it's the ones' complement operator, because it
complements many ones. "Two's complement", however, is correct.

I don't know if that is some of that famous British humour or what.

Nope.

It is the one's complement operator,

No, it's the ones' complement operator. Observe the position of the
apostrophe. If you disagree, take it up with Knuth (TAOCP Vol 2,
Section 4.1).

it does *not* form a proper two's complement.

I didn't say it did. I was referring to your correct positioning of
the apostrophe in "two's complement", later in your article.

Some of your alleged humour is on a par with your food.

I say again, there is no humour intended here. I am being perfectly
serious. I don't know what amusement *you* see in this discussion, but
I'm not seeing any at all.
>If I had not
responded an innocent passerby who didn't try to parse your response,
as I did, could be misled badly.

I don't see how. You said "Tilde is the one's complement operator" and I
replied "No, it's the ones' complement operator". The correction seems
plain enough to me.

If you think my correction is spurious, I invite you to check the Knuth
reference I gave.
It has nothing to do with the legitimacy of what you are saying. You know,
this English language is expressive enough to actually say what you mean
instead of hiding the meaning behind a veil. I see no point whatsoever in
being so subtle on such a thing, as I said, your initial post could easily
mislead someone.

FWIW, K&R spell it my way. My work is done here.
Jun 19 '07 #10

P: n/a
On 2007-06-19, osmium <r1********@comcast.netwrote:
"Shraddha" writes:
>>
for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...

Tilde is the one's complement operator. I would not think of that as
bulletproof code, the results might well be different on a one's complement
machine than they are on a two's complement machine. The result is
"negative zero" on a one's complement machine.
Given the presence of the shift operator, I would have thought that it
is more important to the author that i has the all ones bit pattern
that it represents any particular number. For this reason ~0 is
initially a more logical choice than -1.

We don't know what i is, but whether the loop will terminates if it is
signed is implementation defined. If the implementation uses a two's
complement representation and the shift operator for signed ints on
the implementation does an arithmetic right shift (shifts in the sign
bit and leaves the sign bit unchanged) then the loop will not
terminate.

For this reason, if this is to be portable code, i should be unsigned.
If i is unsigned then -1 and ~0 are both equivalent as both are
guaranteed to represent 2^N - 1.

OK, the last paragraph isn't true :). Technically -1 and ~0U are
guaranteed to represent 2^N - 1 where N is the number of value bits
in the representation of unsigned int.

OK, the last paragraph isn't true :). If i is wider than an int
(supposed it is an unsigned long) then ~0UL is required. In general,
the constant 0 needs to be widened to the correct width before the ~
operator is applied.
Jun 19 '07 #11

P: n/a
osmium said:
"Richard Heathfield" wrote:
>osmium said:
<snip>
>>
>>If I had not
responded an innocent passerby who didn't try to parse your
response,
as I did, could be misled badly.

I don't see how. You said "Tilde is the one's complement operator"
and I replied "No, it's the ones' complement operator". The
correction seems plain enough to me.

If you think my correction is spurious, I invite you to check the
Knuth reference I gave.

It has nothing to do with the legitimacy of what you are saying. You
know, this English language is expressive enough to actually say what
you mean instead of hiding the meaning behind a veil. I see no point
whatsoever in being so subtle on such a thing
Nor do I. Had I intended to be subtle, I would have *been* subtle.
as I said, your initial post could easily mislead someone.
Only someone easily misled. Whilst I agree that there is no shortage of
such people, they probably wouldn't make very good C programmers
anyway.
FWIW, K&R spell it my way.
Fine, so submit a bug report.
My work is done here.
Er, okay, if you say so.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 19 '07 #12

P: n/a
On Mon, 18 Jun 2007 20:06:48 -0700, in comp.lang.c , "osmium"
<r1********@comcast.netwrote:
>
Some of your alleged humour is on a par with your food.
I'm curious as to how often you've eaten at the Heathfield
household....
>If I had not
responded an innocent passerby who didn't try to parse your response, as I
did, could be misled badly.
I doubt it.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 19 '07 #13

P: n/a
On Mon, 18 Jun 2007 20:27:36 -0700, in comp.lang.c , "osmium"
<r1********@comcast.netwrote:
>FWIW, K&R spell it my way.
So ? That merely tells you the quality of proofreading. You would not
believe some of the dreck that my missus has had to send back to
alleged proofreaders.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 19 '07 #14

P: n/a
In article <PN******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Tilde is the one's complement operator.
>Actually, it's the ones' complement operator, because it complements
many ones.
Presumably you mean "with respect to many ones". At least, that's
what Knuth says, and I believe it's from him that this view derives.
>"Two's complement", however, is correct.
.... because, so it is claimed, it complements with respect to a single
power of two.

But I find this argument unconvincing. The apostrophe suggests that
they are complements *of* one and two, which they aren't. It would be
reasonably natural to refer to the one- or two-complement of a single
bit, and extended the terms unchanged to cover the multi-bit case.

But in any case the term "twos complement", regardless of apostrophe,
does not capture the key point of that representation. It is the
truncated form of the result of applying the usual binary arithmetic
to a conceptually infinite strings of bits. If you blindly subtract 1
from 0 in binary, you get ones all the way.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 19 '07 #15

P: n/a
Richard Tobin said:
In article <PN******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>Tilde is the one's complement operator.
>>Actually, it's the ones' complement operator, because it complements
many ones.

Presumably you mean "with respect to many ones".
Call me Mr I Didn't Get Any Sleep Last Night. You're right - that's what
I meant.
At least, that's
what Knuth says, and I believe it's from him that this view derives.
Quite so.
>
>>"Two's complement", however, is correct.

... because, so it is claimed, it complements with respect to a single
power of two.

But I find this argument unconvincing.
Well, then, I can only suggest that you attempt to claim your $2.56 from
DEK. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 19 '07 #16

P: n/a
Joe Wright wrote:
Shraddha wrote:
>I got a small programm on net...but there was different
initialisation that I saw...

It was as follows:

for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...

Same as "i = -1;" I suppose.
Nope. The effect will depend on the number representation, and is
thus not constant. Bad.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 19 '07 #17

P: n/a
In article <j3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>On Mon, 18 Jun 2007 20:06:48 -0700, in comp.lang.c , "osmium"
<r1********@comcast.netwrote:
>>
Some of your alleged humour is on a par with your food.

I'm curious as to how often you've eaten at the Heathfield
household....
If the food is as good as the humour, I should definitely make plans to
do so in the (unlikely) event I'm ever on that side of the world.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
>I can certainly imagine C370 programmers living in their own world.
It's very dark in there, too; no windows...
--Lawrence Kirby and Richard Heathfield in comp.lang.c
Jun 19 '07 #18

P: n/a
dj******@csclub.uwaterloo.ca (Dave Vandervies) writes:
In article <j3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>>On Mon, 18 Jun 2007 20:06:48 -0700, in comp.lang.c , "osmium"
<r1********@comcast.netwrote:
>>>Some of your alleged humour is on a par with your food.

I'm curious as to how often you've eaten at the Heathfield
household....

If the food is as good as the humour, I should definitely make plans to
do so in the (unlikely) event I'm ever on that side of the world.
If his food is like his humo[u]r, I fear it may be too dry.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 19 '07 #19

P: n/a
Barry Schwarz wrote:
On Mon, 18 Jun 2007 20:28:36 -0400, Joe Wright
<jo********@comcast.netwrote:
>Shraddha wrote:
>>I got a small programm on net...but there was different initialisation
that I saw...

It was as follows:

for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...
Same as "i = -1;" I suppose.

Not on a 1's complement or a signed magnitude machine.
Of course but these machines imaginary aren't they?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 19 '07 #20

P: n/a
osmium wrote:
"Richard Heathfield" writes:
> osmium said:
>>"Richard Heathfield" writes:
osmium said:

<snip>

Tilde is the one's complement operator.

Actually, it's the ones' complement operator, because it
complements many ones. "Two's complement", however, is correct.

I don't know if that is some of that famous British humour or
what.

Nope.
>>It is the one's complement operator,

No, it's the ones' complement operator. Observe the position of
the apostrophe. If you disagree, take it up with Knuth (TAOCP
Vol 2, Section 4.1).
>>it does *not* form a proper two's complement.

I didn't say it did. I was referring to your correct positioning
of the apostrophe in "two's complement", later in your article.

Some of your alleged humour is on a par with your food. If I had
not responded an innocent passerby who didn't try to parse your
response, as I did, could be misled badly.
What humor? I think he is perfectly serious. Grammar counts.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jun 19 '07 #21

P: n/a
Joe Wright wrote:
Barry Schwarz wrote:
><jo********@comcast.netwrote:
>>Shraddha wrote:
.... snip ...
>>>>
right shift is ok..But what is meaned by " i = ~0 "...

Same as "i = -1;" I suppose.

Not on a 1's complement or a signed magnitude machine.

Of course but these machines imaginary aren't they?
To the same extent that -1 is.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 19 '07 #22

P: n/a
CBFalconer wrote:
Joe Wright wrote:
>Barry Schwarz wrote:
>><jo********@comcast.netwrote:
Shraddha wrote:
... snip ...
>>>>right shift is ok..But what is meaned by " i = ~0 "...
>
Same as "i = -1;" I suppose.
Not on a 1's complement or a signed magnitude machine.
Of course but these machines imaginary aren't they?

To the same extent that -1 is.
This is a very tough room. Let me try again. The question..
"what is meaned by " i = ~0 "..." might translate to..

int i = ~0;

...What goes on here?

The 0 of course in an integer value with all bits 0. The prefix tilde
performs a binary complement of the value, in this case to all bits 1.

The value of 'all bits 1' depends on how you look at it.

Two's complement suggests -1.
One's complement suggests -0.
Signed Magnitude maybe minus INT_MAX?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 19 '07 #23

P: n/a
On Tue, 19 Jun 2007 13:29:20 -0400, in comp.lang.c , Joe Wright
<jo********@comcast.netwrote:
>Barry Schwarz wrote:
>Not on a 1's complement or a signed magnitude machine.
Of course but these machines imaginary aren't they?
Well, certainly some of them are now extraterrestrial

http://en.wikipedia.org/wiki/Apollo_Guidance_Computer

while others appeared out of the blue, as it were...

http://en.wikipedia.org/wiki/IBM_7090
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 19 '07 #24

P: n/a
Joe Wright wrote:
CBFalconer wrote:
>Joe Wright wrote:
>>Barry Schwarz wrote:
<jo********@comcast.netwrote:
Shraddha wrote:
... snip ...
>>>>>right shift is ok..But what is meaned by " i = ~0 "...
>>
Same as "i = -1;" I suppose.
Not on a 1's complement or a signed magnitude machine.
Of course but these machines imaginary aren't they?

To the same extent that -1 is.
This is a very tough room. Let me try again. The question..
"what is meaned by " i = ~0 "..." might translate to..

int i = ~0;
Who says i is declared as int? The original poster didn't, as far as I've
seen. If i is not declared as int, it is important to remember than ~0 is
evaluated as type int (the type of 0), and the result is converted to the
type of i. That is,

unsigned i = ~0;

is not equivalent to

unsigned i = 0;
i = ~i;

even though in this case, for two's complement, the result will be the same.
..What goes on here?

The 0 of course in an integer value with all bits 0. The prefix tilde
performs a binary complement of the value, in this case to all bits 1.

The value of 'all bits 1' depends on how you look at it.

Two's complement suggests -1.
Yes.
One's complement suggests -0.
As was mentioned elsethread, it's called "ones' complement".

-0 is never negative zero. If negative zero is supported, it will compare
equal to plain zero, but you don't get negative zero as a result of any
non-bitwise arithmetic operation, including negation. -0 is equivalent to
+0 .

And ones' complement systems are allowed to say the behaviour is undefined
if ~0 is evaluated. Negative zero need not be supported, and if it is not,
then all bits set is a trap representation.
Signed Magnitude maybe minus INT_MAX?
It's called "sign and magnitude", but right. Which of course equals INT_MIN
for these systems.
Jun 19 '07 #25

P: n/a
Joe Wright wrote:
Barry Schwarz wrote:
>On Mon, 18 Jun 2007 20:28:36 -0400, Joe Wright
<jo********@comcast.netwrote:
>>Shraddha wrote:
I got a small programm on net...but there was different initialisation
that I saw...

It was as follows:

for ( i = ~0 ; i ; i>>=1);

right shift is ok..But what is meaned by " i = ~0 "...

Same as "i = -1;" I suppose.

Not on a 1's complement or a signed magnitude machine.
Of course but these machines imaginary aren't they?
Of course they are. Completely imaginary. And THAT'S
why the committees who wrote and revised the Standards made
allowance for them. Even when they tightened the allowable
representations in C99, they still allowed ones' complement
and signed magnitude. Simply decreeing two's complement
would have made a lot of things simpler, but the committees
were made up entirely of masochists who loved to make trouble
for themselves. That's why you needn't worry your pretty
little head about it: All the world is a two's complement
32-bit Little-Endian IEEE-floating-point dosbox, and don't
let anyone tell you any different.

Or to put it another way: A friend of mine likes to say
that computing is a fashion-driven industry. Fashions come,
and fashions go, and fashions come again. You can write your
program for this season's hem lines and color schemes, or you
can attempt something a little less transitory. There are,
after all, some distinct infelicities in two's complement:
the fact that abs(n) cannot be implemented reliably stands
out as an ugliness that neither of the alternatives suffer
from, and tomorrow's system couturier may reject that ugliness.
Trap representations for integers are Out today, but in the
face of viral onslaught might not tomorrow's machine revive
the old practice of explicitly-typed data?

Sometimes it doesn't make sense to sacrifice all other goals
on the altar of portability: Portability is just one among many
desirable attributes of a program. But there are *very* few
reasons to engage in gratuitous importabilities like those(?)
in the above code example. You may not particularly like the
ruffles-with-polka-dots theme that all the designers will put
on the runways next year, but it costs you little or nothing
to accommodate it -- and if you don't, none of the hot models
will wear your code.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 20 '07 #26

P: n/a
Eric Sosman <es*****@acm-dot-org.invalidwrote:
Or to put it another way: A friend of mine likes to say
that computing is a fashion-driven industry. Fashions come,
and fashions go, and fashions come again. You can write your
program for this season's hem lines and color schemes, or you
can attempt something a little less transitory. There are,
after all, some distinct infelicities in two's complement:
the fact that abs(n) cannot be implemented reliably stands
out as an ugliness that neither of the alternatives suffer
from, and tomorrow's system couturier may reject that ugliness.
Trap representations for integers are Out today, but in the
face of viral onslaught might not tomorrow's machine revive
the old practice of explicitly-typed data?
Mind you, for the fashion industry it seems to be lucrative to work only
to today's fashion, and let the airheaded customers buy each year's
fashion in turn. It doesn't seem to do Microsoft any harm, either.

Whether that is a business model you wish to strive for is on each
programmer's own conscience. But lucrative it is.

Richard
Jun 20 '07 #27

This discussion thread is closed

Replies have been disabled for this discussion.