473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

D

Is it not time to make a new language D drawing on the experiences from C
and C++?

Specifically, such a language should zip out the parts of C that are
problematic and caused ambiguities and problems in C++. For example, the
many implicit type conversions and problems in the C syntax which are
difficult handle in a C++ high level language style. (There is an example
in the Bison manual in the GLR parser section of a C++ language
ambiguity).

Then one would first get a cleaner C core, not having to worry about
upwards compatibility so much anymore. Further this core can be designed
with respect to more modern CPU structures in mind.

The experiences from C++ will tell mainly how to implement statically
computable user definable structures, including some generic programming
then.

To this, one needs a level above the C++ level, handling parallelism,
distributed programming and better handling of dynamic objects (so it
becomes easier to create Java style polymorphic classes), including ones
choice of GC if needed.

On the level below the C level, one can think of a "portable assembler
level" like C-- and/or a byte code level, which can be used as an
intermediate to an assembler level. This level should also provide hooks
to "universal binary standards" like CORBA and Unicode, which C and C++
carefully avoids due to their history.

See you in comp.std.d. :-)

Hans Aberg * Anti-spam: remove "remove." from email address.
* Email: Hans Aberg <re***********@member.ams.org>
* Home Page: <http://www.math.su.se/~haberg/>
* AMS member listing: <http://www.ams.org/cml/>
Nov 13 '05
69 3404
Kevin Easton wrote:

In comp.lang.c Paul Hsieh <qe*@pobox.com> wrote:
co****@panix.com (Greg Comeau) wrote in message
news:<be**********@panix5.panix.com>...
[...] Many features in many languages are easy to misunderstand,
including in C, and do sometimes require a closer look.


You're right -- I'm sure there's a primitive rotate instruction in
there somewhere that I'm just not seeing.


Yes, it's quite cunningly hidden:

unsigned rot(unsigned n, int m)
{
return n >> (32-m) | n << m;
}


If you can't write it portably, then you're making the point
that a primative rotate instruction is not part of C.
Is that what you're getting at ?

--
pete
Nov 13 '05 #51

On Mon, 14 Jul 2003, pete wrote:

Kevin Easton wrote:

In comp.lang.c Paul Hsieh <qe*@pobox.com> wrote:

You're right -- I'm sure there's a primitive rotate instruction in
there somewhere that I'm just not seeing.
Yes, it's quite cunningly hidden:

unsigned rot(unsigned n, int m)
{
return n >> (32-m) | n << m;
}


If you can't write it portably,


[Note that he *could* have made it portable by clever use of UINT_MAX.]
then you're making the point
that a primitive rotate instruction is not part of C.
Is that what you're getting at ?


Yes. His point, as I understand it, was two-fold: first, that since
the "rotate" opcode itself is not portable, it doesn't make sense to
include it in [portable] C; and second, that on platforms which have
a "rotate" opcode to use, any decent optimizer will take care of it
for you. So Paul's "efficiency" argument is bogus.

Tested on DJGPP and Linux gcc for Intel x86 (worked), and gcc for
Solaris 4.something (didn't). I don't know whether the Sun architecture
has 'rotate' or not, but it still compiled to three instructions (with
mnemonics 'srl', 'sll', and 'or').

The details of specific optimizers and machine architectures are obviously
OT here, but it's also pretty clear that gcc -O3 doesn't need any help
from Paul, thankyouverymuch. ;-)

-Arthur

Nov 13 '05 #52
In comp.lang.c pete <pf*****@mindspring.com> wrote:
Kevin Easton wrote:

In comp.lang.c Paul Hsieh <qe*@pobox.com> wrote:
> co****@panix.com (Greg Comeau) wrote in message
> news:<be**********@panix5.panix.com>... >> [...] Many features in many languages are easy to misunderstand,
>> including in C, and do sometimes require a closer look.
>
> You're right -- I'm sure there's a primitive rotate instruction in
> there somewhere that I'm just not seeing.


Yes, it's quite cunningly hidden:

unsigned rot(unsigned n, int m)
{
return n >> (32-m) | n << m;
}


If you can't write it portably, then you're making the point
that a primative rotate instruction is not part of C.
Is that what you're getting at ?


It is portable, in that it gets the same mathematical result anywhere
(well, if I'd used unsigned long, or 16 instead of 32 there). It also
can (and commonly is) optimised down to a single "rotate" machine
instruction, on platforms where that will get the correct mathematical
result (what would be the point of a new operator that *didn't* get the
same result everywhere? It'd be like another signed-right-shift).

- Kevin.

Nov 13 '05 #53
aj*@andrew.cmu.edu says...
On Mon, 14 Jul 2003, pete wrote:
Kevin Easton wrote:
In comp.lang.c Paul Hsieh <qe*@pobox.com> wrote:
> You're right -- I'm sure there's a primitive rotate instruction in
> there somewhere that I'm just not seeing.

Yes, it's quite cunningly hidden:

unsigned rot(unsigned n, int m)
{
return n >> (32-m) | n << m;
}
If you can't write it portably,


[Note that he *could* have made it portable by clever use of UINT_MAX.]


That's not what he's missing. He's missing a couple of "& 31"'s (%32 would be
incorrect since they might output negative numbers.)
then you're making the point
that a primitive rotate instruction is not part of C.
Is that what you're getting at ?


Yes. His point, as I understand it, was two-fold: first, that since
the "rotate" opcode itself is not portable, it doesn't make sense to
include it in [portable] C; and second, that on platforms which have
a "rotate" opcode to use, any decent optimizer will take care of it
for you. So Paul's "efficiency" argument is bogus.


That's not true because ...
Tested on DJGPP and Linux gcc for Intel x86 (worked), and gcc for
Solaris 4.something (didn't).
.... WATCOM, Intel, and Microsoft all fail to do it (I highly doubt that Borland
does it either, but optimization and Borland are oxymorons.) Of course I don't
blame them, the source given does not imply that a rotate instruction is the
necessary output.
The details of specific optimizers and machine architectures are obviously
OT here, but it's also pretty clear that gcc -O3 doesn't need any help
from Paul, thankyouverymuch. ;-)


Gcc's ability to translate undefined code and make it into something useful is
legendary. But even if by some tragedy gcc becomes the defacto standard
compiler that everyone uses, the performance problems will only get *WORSE*,
not better.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sourceforge.net/
Nov 13 '05 #54
In article <ne********************@tomato.pcug.org.au>, kevin@-nospam-
pcug.org.au says...
In comp.lang.c pete <pf*****@mindspring.com> wrote:
Kevin Easton wrote:
In comp.lang.c Paul Hsieh <qe*@pobox.com> wrote:
> co****@panix.com (Greg Comeau) wrote in message
> news:<be**********@panix5.panix.com>...
>> [...] Many features in many languages are easy to misunderstand,
>> including in C, and do sometimes require a closer look.
>
> You're right -- I'm sure there's a primitive rotate instruction in
> there somewhere that I'm just not seeing.

Yes, it's quite cunningly hidden:

unsigned rot(unsigned n, int m)
{
return n >> (32-m) | n << m;
}


If you can't write it portably, then you're making the point
that a primative rotate instruction is not part of C.
Is that what you're getting at ?


It is portable, in that it gets the same mathematical result anywhere
(well, if I'd used unsigned long, or 16 instead of 32 there).


The size is only one of your issues. The C standard does not guarantee
anything about shifting past the size of integer in bits. While you can make
an argument about not needing to shift an integer beyond its size in bits,
that's *NOT* true about rotates.
[...] It also can (and commonly is) [...]


Its not as common as you think.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sourceforge.net/
Nov 13 '05 #55
In article <MP************************@news.sf.sbcglobal.ne t>
Paul Hsieh <qe*@pobox.com> writes:
If I want a platform specific rotate then I'll just write:

int rot(int,char);
#pragma aux rot = "rol eax, cl" parm [eax] [cl] value [eax];

thank you very much.


But this does not work at all -- I wanted a 24-bit rotate!

Seriously, has no one yet noticed that "rotate" is inherently a
three-input operation? We have the bit mask to rotate, the number
of bits in that bit mask, and the number of bits by which the rotate
should happen.

Of course, all this "rotate" stuff is a red herring; the *correct*
primitive is "funnel shift", which is a *four*-input operation
(left side, right side, number of bits in each side, and position
of selector). Rotation is obtained by making the left and right
sides identical, and barrel-shifting is obtained by making one of
"left side" or "right side" all-zero-bits.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #56
Chris Torek wrote:
Seriously, has no one yet noticed that "rotate" is inherently a
three-input operation? We have the bit mask to rotate, the number
of bits in that bit mask, and the number of bits by which the rotate
should happen.
As soon as one wants to implement bignums, one notices this.
Of course, all this "rotate" stuff is a red herring; the *correct*
primitive is "funnel shift", which is a *four*-input operation
(left side, right side, number of bits in each side, and position
of selector). Rotation is obtained by making the left and right
sides identical, and barrel-shifting is obtained by making one of
"left side" or "right side" all-zero-bits.


Could you expand on this? I'm guessing the effect is something like:

(leftSide in numBits bits) concatenate (rightSide in numBits bits)
then extractBits [from selectorPosition width numBits]

?

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #57
>Chris Torek wrote:
Of course, all this "rotate" stuff is a red herring; the *correct*
primitive is "funnel shift", which is a *four*-input operation
(left side, right side, number of bits in each side, and position
of selector). Rotation is obtained by making the left and right
sides identical, and barrel-shifting is obtained by making one of
"left side" or "right side" all-zero-bits.

In article <bf**********@murdoch.hpl.hp.com>
Chris Dollin <ke**@hpl.hp.com> writes:Could you expand on this? I'm guessing the effect is something like:

(leftSide in numBits bits) concatenate (rightSide in numBits bits)
then extractBits [from selectorPosition width numBits]

?


Right. Using this primitive, we can get shifts, rotates, and
field-extractions within multiword values. For instance, if you
wants bits 27 through 38 inclusive, as a 12-bit sub-field within
a 64-bit number, you simply concatenate the left and right 32-bit
numbers and extract 12 bits at position 27.

Some CPUs have direct access to funnel shifters; others use them
internally to implement shifts, rotates, and extractions.

(Bitfield insertion still requires read/modify/writeback, no matter
what you do, though.)
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #58
In comp.lang.c Paul Hsieh <qe*@pobox.com> wrote:
kevin@-nospam-pcug.org.au says...
In comp.lang.c Paul Hsieh <qe*@pobox.com> wrote:
> co****@panix.com (Greg Comeau) wrote:
>> [...] Many features in many languages are easy to misunderstand,
>> including in C, and do sometimes require a closer look.
>
> You're right -- I'm sure there's a primitive rotate instruction in
> there somewhere that I'm just not seeing.
Yes, it's quite cunningly hidden:

unsigned rot(unsigned n, int m)
{
return n >> (32-m) | n << m;
}


Odd ... this will not actually rotate the bits correctly. Consider rot(1, 80)
for example. Shift on Intel processors happen to mod the shift value by the
size of the word so it actually works. However, according to the ANSI C
standard, there is no requirement for it to do that. Technically, this code is
not portable.


Well, I didn't state the domain of the function (... m should be in the
range 0 to 31 inclusive for this 32 bit left rotate). Replacing the ms
with (m & 0x1f) and the "unsigned" with "unsigned long" is easy enough,
though, if that's what you want.
gcc -S foo.c
cat foo.s

rot:
pushl %ebp
movl %esp,%ebp
movl 12(%ebp),%ecx
movl 8(%ebp),%edx
roll %cl,%edx
movl %edx,%eax
jmp .L2
.p2align 4,,7
.L2:
leave
ret

Have a bit more faith in the optimiser!


But this is just a fluke. shifting beyond the size of the word in bits is not
well defined. So while its *legal* for GCC to output this code, there is no
guarantee that it, or any other compiler (even on machines with 32 bit
integers) will output code which performs a proper 32 bit rotate.


Since the expression, over the domain in which its behaviour *is*
defined, is completely simulated by this platform's hardware rol
instruction, we're entitled to expect that the optimiser should
recognize this. The behaviour of the hardware rol instruction outside
the domain in which the function has defined behaviour is of course of
no importance (to this optimisation).
If I want a platform specific rotate then I'll just write:

int rot(int,char);
#pragma aux rot = "rol eax, cl" parm [eax] [cl] value [eax];


The overarching point being, that if you did have a rotate left bitwise
operator in C (shift-expression |< additive-expression maybe?), then
it's mathematical behaviour would depend entirely on the width of the
type you're working in, which would in practice make it as useless as
signed right shifts.

On the other hand, the next revision of C should certainly include some
vector operations, which can be optimised easily to SIMD instructions on
platforms that have those.

- Kevin.

Nov 13 '05 #59

"Paul Hsieh" <qe*@pobox.com> wrote in message news:MP************************@news.sf.sbcglobal. net...
Odd ... this will not actually rotate the bits correctly. Consider rot(1, 80)
for example


Well, one might argue that rotates of values greater than the number of bits
are undefined behavior just as shifts are in C and C++.

Nov 13 '05 #60
On Tue, 15 Jul 2003, Kevin Easton wrote:
Odd ... this will not actually rotate the bits correctly. Consider rot(1, 80)
for example. Shift on Intel processors happen to mod the shift value by the
size of the word so it actually works. However, according to the ANSI C
standard, there is no requirement for it to do that. Technically, this code is
not portable.


Well, I didn't state the domain of the function (... m should be in the
range 0 to 31 inclusive for this 32 bit left rotate). Replacing the ms
with (m & 0x1f) and the "unsigned" with "unsigned long" is easy enough,
though, if that's what you want.


Problem: while gcc can indeed rewrite x>>n|x<<32-n as a rotate, it doesn't
remove the &31 in x>>(n&31).

I mean really: play nice and add the &31 there making it portable just
to see it actually performs the no-op. sigh.

BTW, ITYM 1 to 31 inclusive.

Nov 13 '05 #61

On Tue, 15 Jul 2003, Paul Hsieh wrote:

In article <bf**********@elf.eng.bsdi.com>, no****@elf.eng.bsdi.com says...
Paul Hsieh <qe*@pobox.com> writes:
If I want a platform specific rotate then I'll just write:

int rot(int,char);
#pragma aux rot = "rol eax, cl" parm [eax] [cl] value [eax];

thank you very much.
But this does not work at all -- I wanted a 24-bit rotate!
More likely 28. ;-)
Seriously, has no one yet noticed that "rotate" is inherently a
three-input operation? We have the bit mask to rotate, the number
of bits in that bit mask, and the number of bits by which the rotate
should happen.

Of course, all this "rotate" stuff is a red herring; the *correct*
primitive is "funnel shift", which is a *four*-input operation
(left side, right side, number of bits in each side, and position
of selector). Rotation is obtained by making the left and right
sides identical, and barrel-shifting is obtained by making one of
"left side" or "right side" all-zero-bits.


Your ill attempt at humor is [a non sequitur].


Not really humor. More like a technical description of the semantics
of the family of operations you've been calling "shift" and "rotate."
It's perfectly true that x86-style fixed-width rotate instructions
are fairly useless, compared to the "funnel shift" operation Chris
describes. Of course, with more than two inputs it's hard to make
this operation a primitive "operator" in C; that's why you don't want
to think about it. I still haven't figured out why you really *want*
a (or several) "rotate operator(s)" when an inline function is just
as fast, and can take the multiple inputs it needs to do the job right,
plus specializations for values of 'n' or 'b' that are easy to do
on whatever architecture you're targeting.
Ordinary rotates happen a lot.
Define "ordinary." Do you perhaps mean "32-bit rotates, with the
right-hand operator truncated modulo 32 and stored in the CL register"?
(--Now, that *was* an ill attempt at humor.)
Name me one feistel crypto algorithm that *DOESN'T* use a rotate. DES,
BlowFish, MD5, they *all* use rotate.


Blowfish does not use a rotate.
http://www.counterpane.com/bfdobsoyl.html

DES uses a 28-bit rotate.
http://www.aci.net/kalliste/des.htm

MD5 actually does use a 32-bit rotate. Congrats!
http://www.ietf.org/rfc/rfc1321.txt

[I am not a cryptography expert.]
-Arthur
Nov 13 '05 #62
jw******@cs.Helsinki.FI says...
On Tue, 15 Jul 2003, Kevin Easton wrote:
Odd ... this will not actually rotate the bits correctly. Consider rot(1, 80)
for example. Shift on Intel processors happen to mod the shift value by the
size of the word so it actually works. However, according to the ANSI C
standard, there is no requirement for it to do that. Technically, this code is
not portable.


Well, I didn't state the domain of the function (... m should be in the
range 0 to 31 inclusive for this 32 bit left rotate). Replacing the ms
with (m & 0x1f) and the "unsigned" with "unsigned long" is easy enough,
though, if that's what you want.


Problem: while gcc can indeed rewrite x>>n|x<<32-n as a rotate, it doesn't
remove the &31 in x>>(n&31).

I mean really: play nice and add the &31 there making it portable just
to see it actually performs the no-op. sigh.


None of my compilers, including one extremely state of the art one (Intel C++),
fails to perform this transformation regardless of what I do. There is an
outside chance that by 2005 this compiler will pick up this capability, but
that's another story ...

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sourceforge.net/
Nov 13 '05 #63
Ron Natalie wrote:
"Paul Hsieh" <qe*@pobox.com> wrote in message news:MP************************@news.sf.sbcglobal. net...
Odd ... this will not actually rotate the bits correctly. Consider rot(1, 80)
for example


Well, one might argue that rotates of values greater than the number of bits
are undefined behavior just as shifts are in C and C++.


Or one might just argue the opposite. Are you worried that the
CPU will become dizzy after some arbitrary number of bits rotation?

8^)
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #64
qe*@pobox.com says...
jw******@cs.Helsinki.FI says...
On Tue, 15 Jul 2003, Kevin Easton wrote:
Well, I didn't state the domain of the function (... m should be in the
range 0 to 31 inclusive for this 32 bit left rotate). Replacing the ms
with (m & 0x1f) and the "unsigned" with "unsigned long" is easy enough,
though, if that's what you want.


Problem: while gcc can indeed rewrite x>>n|x<<32-n as a rotate, it doesn't
remove the &31 in x>>(n&31).

I mean really: play nice and add the &31 there making it portable just
to see it actually performs the no-op. sigh.


None of my compilers, including one extremely state of the art one (Intel C++),
fails to perform this transformation regardless of what I do.


Sorry, I meant to write that *ALL* of my compilers fail to do this
transformation. I don't have/use gcc or its variants, so I can't verify that
one way or another.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sourceforge.net/
Nov 13 '05 #65
ro*@sensor.com says...
"Paul Hsieh" <qe*@pobox.com> wrote:
Odd ... this will not actually rotate the bits correctly. Consider rot(1, 80)
for example


Well, one might argue that rotates of values greater than the number of bits
are undefined behavior just as shifts are in C and C++.


You can, but you'd be wrong. Rotations are operations which form a
mathematical object called a "group" -- but this is really only true if the
rotation index is unrestricted, or equivalently that the modulo on the rotation
index is applied. This is not true of shifts, which is why the argument is
different.

In essence, ror(ror(a,b),c) should be simplifiable to ror(a,b+c) without
question. (I could argue for different kinds properties for shifts, but they
are not the same as this one, and obviously the ANSI C committee isn't
concerned about such things.)

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sourceforge.net/
Nov 13 '05 #66
>ro*@sensor.com says...
Well, one might argue that rotates of values greater than the number of bits
are undefined behavior just as shifts are in C and C++.

In article <MP************************@news.sf.sbcglobal.ne t>
Paul Hsieh <qe*@pobox.com> writes:You can, but you'd be wrong. Rotations are operations which form a
mathematical object called a "group" -- but this is really only true
if the rotation index is unrestricted, or equivalently that the modulo
on the rotation index is applied.
Yes, but -- mod WHAT?
In essence, ror(ror(a,b),c) should be simplifiable to ror(a,b+c) without
question.


Only if the number of bits being rotated matches.

In particular, if you want to do a 28-bit "rotation by 4", followed
by a 16-bit "rotation by 4", you *cannot* simply do a 28-bit rotation
by 8. (You will get the wrong answer.)

Note that in assembly, we might write:

ror.l %r1,6
rol.w %r1,4

to take a 32-bit input value in register 1 of the form:

abcdefgh.ijklmnop.qrstuvwx.yzABCDEF

and get:

ABCDEFab.cdefghij.opqrstuv.wxyzklmn

(where each letter represents a single bit).

Of course, as I noted earlier, I would rather have a funnel-shift
operation than merely a rotate operation.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #67
>But how implement operator+ *without a copy* in a C++ class, or in C?

class num1 (if I read well) doesn't use copy (I use &) but don't work
because if I do
num1 x, a("100"), b("777");
x= a*b;
although inside operator* seems to be ok in operator= is KO. Why?
class num10 seems ok;

class num10{
friend int operator==(const num10& a, const num10& b);
friend int operator<( const num10& a, const num10& b);
friend int operator>( const num10& a, const num10& b);
friend int operator<=(const num10& a, const num10& b);
friend int operator>=(const num10& a, const num10& b);
friend int operator!=(const num10& a, const num10& b);

friend int operator==(const num10& a, unsigned b);
friend int operator<( const num10& a, unsigned b);
friend int operator>( const num10& a, unsigned b);
friend int operator<=(const num10& a, unsigned b);
friend int operator>=(const num10& a, unsigned b);
friend int operator!=(const num10& a, unsigned b);

friend int operator==(unsigned a, const num10& b);
friend int operator<( unsigned a, const num10& b);
friend int operator>( unsigned a, const num10& b);
friend int operator<=(unsigned a, const num10& b);
friend int operator>=(unsigned a, const num10& b);
friend int operator!=(unsigned a, const num10& b);

friend num10 operator+( const num10& a, const num10& b);
friend num10 operator+( const num10& a, unsigned b);
friend num10 operator+( unsigned b, const num10& a);

friend num10 operator-( const num10& a, const num10& b);
friend num10 operator-( const num10& a, unsigned b);
friend num10 operator-( unsigned b, const num10& a);

friend num10 operator*( const num10& a, const num10& b);
friend num10 operator*( const num10& a, unsigned b);
friend num10 operator*( unsigned b, const num10& a);

friend num10 operator/( const num10& a, const num10& b);
friend num10 operator/( const num10& a, unsigned b);
friend num10 operator/( unsigned b, const num10& a);

friend num10 operator%( const num10& a, const num10& b);
friend num10 operator%( const num10& a, unsigned b);
friend num10 operator%( unsigned b, const num10& a);
friend int resize( unsigned len);

friend void pr_num( const num10& a );
public :
num10();
num10(unsigned len);
num10(const char a[]);
num10(num10& r);
num10& operator=(const num10& r);
num10& operator=(unsigned r);
~num10();

numero* nume() { R &numer; }

unsigned char* ricon1();
void print();

private:
numero numer;
unsigned lung;
};

class num1{
friend int operator==(const num1& a, const num1& b);
friend int operator<( const num1& a, const num1& b);
friend int operator>( const num1& a, const num1& b);
friend int operator<=(const num1& a, const num1& b);
friend int operator>=(const num1& a, const num1& b);
friend int operator!=(const num1& a, const num1& b);

friend int operator==(const num1& a, unsigned b);
friend int operator<( const num1& a, unsigned b);
friend int operator>( const num1& a, unsigned b);
friend int operator<=(const num1& a, unsigned b);
friend int operator>=(const num1& a, unsigned b);
friend int operator!=(const num1& a, unsigned b);

friend int operator==(unsigned a, const num1& b);
friend int operator<( unsigned a, const num1& b);
friend int operator>( unsigned a, const num1& b);
friend int operator<=(unsigned a, const num1& b);
friend int operator>=(unsigned a, const num1& b);
friend int operator!=(unsigned a, const num1& b);

friend num1& operator+( const num1& a, const num1& b);
friend num1& operator+( const num1& a, unsigned b);
friend num1& operator+( unsigned b, const num1& a);

friend num1& operator-( const num1& a, const num1& b);
friend num1& operator-( const num1& a, unsigned b);
friend num1& operator-( unsigned b, const num1& a);

friend num1& operator*( const num1& a, const num1& b);
friend num1& operator*( const num1& a, unsigned b);
friend num1& operator*( unsigned b, const num1& a);

friend num1& operator/( const num1& a, const num1& b);
friend num1& operator/( const num1& a, unsigned b);
friend num1& operator/( unsigned b, const num1& a);

friend num1& operator%( const num1& a, const num1& b);
friend num1& operator%( const num1& a, unsigned b);
friend num1& operator%( unsigned b, const num1& a);

friend int resize1( unsigned len);
friend num10 conv(const num1& a);
friend num1& sqrt(const num1& a);
friend num1& gcd (const num1& a, const num1& b);

public :
num1();
num1(unsigned len);
num1(const char a[]);
num1(num1& r);

num1& operator=(num1* r);
num1& operator=(const num1& r);
num1& operator=(unsigned r);
~num1();

void print();
void prin();
void casuale(unsigned len);

private:
num_s numer;
unsigned lung;
};

Nov 13 '05 #68
gi******@giuseppe.wwwew (Giuseppe) wrote:
But how implement operator+ *without a copy* in a C++ class, or in C?


class num1 (if I read well) doesn't use copy (I use &) but don't work
because if I do
num1 x, a("100"), b("777");
x= a*b;
although inside operator* seems to be ok in operator= is KO. Why?
class num10 seems ok;


"fixed"
because a*b have a result more large than a and b the operator= call
resize1 to resize the "static" buffers for the new max_dim and in
resize1(). I forgot to copy the 5 tmp numbers of the buffer (I use tmp
numbers in expressions when I call + * \ -. Does The number of tmp
buffers depend of the number of "(" ")" ?)
Thanks

if I have

int z=0

is right

int& f()
{z=7;
return z;
}

or

int z=0
is right
int& f()
{z=7;
return (int&) z;
}

or

int& f()
{int& u=z
u=7;
return u;
}

?
Nov 13 '05 #69
On Tue, 15 Jul 2003 09:18:49 GMT, qe*@pobox.com (Paul Hsieh) wrote:
aj*@andrew.cmu.edu says... <about supposedly idiomatic rotate>
> unsigned rot(unsigned n, int m)
> {
> return n >> (32-m) | n << m;
> }
<snip> [Note that he *could* have made it portable by clever use of UINT_MAX.]


That's not what he's missing. He's missing a couple of "& 31"'s (%32 would be
incorrect since they might output negative numbers.)

Assuming unsigned is 32 bits, or you use uint32_t or equivalent.

% 32U would be safe, and have the putative advantage of generalizing
to machines with non-power-of-two word sizes, if anyone ever starts
making them again, as well as being a slightly more direct
representation of the programmer's actual intent.

<snip> Gcc's ability to translate undefined code and make it into something useful is
legendary. But even if by some tragedy gcc becomes the defacto standard
compiler that everyone uses, the performance problems will only get *WORSE*,
not better.


Of course in this case the code is still *correct* even if not
optimized as by gcc to an actual rotate instruction.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #70

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.