Had an interview

... snip about xors for exchange ...

The essential point is that xor use can cause data loss. Consider "xchange(a, a);" which will destroy the content of a irreversibly.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
 Click to see the full signature
Reply to
CBFalconer
Loading thread data ...

Robert Adsett wrote: {regarding triple xor to exchange data in two variables]

When I was in college a few decades ago, an IBM instructor teaching PL/I, I think, mentioned it as a way to swap two same-size strings. The IBM mainframe computer had a single instruction to do byte-wise xor on a string. Since the strings could be arbitrarily long, that would avoid a long temporary or a loop for byte-wise swapping.

--
Thad
Reply to
Thad Smith

I think I like that maintainer.

Robert

** Posted from
formatting link
**
Reply to
Robert Adsett

Is there much call for swapping strings? I've replaced strings and swapped string references but I don't recall ever swapping strings (which may just mean I don't deal with the kind of program that would swap strings). The restriction on string size would further limit its potential use rather sharply I would think unless you could do subsets, eve then it would have to be a fairly common operation to be much more than a parlour trick I would think.

Robert

** Posted from
formatting link
**
Reply to
Robert Adsett

Yes, totally acknowledged. I wondered how that portrayed after I'd written it. Apologies.

Honestly though, I've given up pretty much. I'm kept, and I just fiddle about with old computers now. Company politics is something that really frustrates me. Like, if someone wants something done then I'll do it, and if I don't know then I'll find out how. But that doesn't extend so far as to educate everyone else. Explaining to someone how to address an EPROM who works in management is pretty pointless, as it's something you need to understand and almost see in your mind. If you know what I mean.

They think it's easy. It's taken me practically 10-years of ruining things, things that don't work, and trial and error.

The programming is that the layers underneath kept on changing, I stopped after Microsoft kept on moving the goalposts with OO C++. Hence my move to embedded assembly language around the late 90's. C over UNIX though, that's quite nice. Perl/CGI I like too, well, it's practically C anyway. When designing websites I just write a page generator in Perl, which just renders the whole lot in one go. Any changes; you make them in code. It's far more predictable. Cascading Style Sheets are pretty good too, when implemented properly. I don't like server side processing though, it's too open to vulnerbilities. So, Perl the generate the pages offsite. (it produces html source which is almost impossible to read).

Anyway.

Reply to
Aly

This is a standard trick that a good compiler will do for you.

But the swapping vars trick is unlikely to produce optimum ASM in machine that cannot do arithmetic with two memory locations. By the time you have added in the extra move to/from registers, the temp variable option (which the compiler will optimise out) is going to be quicker.

tim

Reply to
tims next home

In message , Aly writes

You mean its social interaction you have a problem with? This is why they do personality tests to see if people will fit in.

So you have pre-judged managers? Sounds like a serious personality defect to me.

You learn by trial and error not using the correct procedures? So that's the difference between programming and SW Engineering

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England     /\/\/\/\/
 Click to see the full signature
Reply to
Chris H

I agree. Unfortunately, there are not enough women in engineering to learn from. I'm sure we (men) miss a lot !

yg

Reply to
whygee

I would never discount the value of trial and error and especially blowing things up. It seems like many of the really significant advances we have made at my company are preceded not by some engineer shouting "Eureka, I found it", but by him muttering "Gee, that's funny".

Mark Walsh

Reply to
Mark Walsh

or, a += b; b = a-b; a -= b;

--
	mac the naïf
Reply to
Alex Colvin

All of these are only valid when the int action on overflow is suitably defined. It isn't. It is implementation defined. In addition the algorithms fail (zeroing everything) when a and b are the same item.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
 Click to see the full signature
Reply to
CBFalconer

You mean the one using addition. The bitwise XOR one should always work.

The one using addition presumably assumes that the 'usual' 2-s complement, wrap-on-overflow behavior is happening. You are correct that this behavior is implementation-specific, but it's probably more ubiquitous these days than compilers that actually adhere to the ANSI standards in every way.

a = 2, b = 2.

a = 2 ^ 2 = 0.

b = 0 ^ 2 = 2.

a = 0 ^ 2 = 2.

Nope.

a = 2, b = 2.

a = 2 + 2 = 4.

b = 4 - 2 = 2.

a = 4 - 2 = 2.

Nope again.

--
Tim Wescott
Control systems and communications consulting
 Click to see the full signature
Reply to
Tim Wescott

He means implemented as a macro or function passed pointers

swap(a, a)

would set a to zero.

Reply to
nospam

Indeed -- if that is what he meant, I certainly misread it.

--
Tim Wescott
Control systems and communications consulting
 Click to see the full signature
Reply to
Tim Wescott

Op Sat, 26 Apr 2008 15:12:57 +0200 schreef DaveN :

Obviously this will give inaccurate results when a+b > INT_MAX.

--
Gemaakt met Opera's revolutionaire e-mailprogramma:  =

http://www.opera.com/mail/
 Click to see the full signature
Reply to
Boudewijn Dijkstra

Op Sat, 26 Apr 2008 17:09:11 +0200 schreef Chris H :=

Not irrelevant at all. uC's that have 64 bytes of RAM, often have just = =

one register.

--
Gemaakt met Opera's revolutionaire e-mailprogramma:  =

http://www.opera.com/mail/
 Click to see the full signature
Reply to
Boudewijn Dijkstra

It takes a nerd to ask that at an interview.

Reply to
Jon

In message , Jon writes

Maybe. I might ask a question like that to see the reaction of the interviewee.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England     /\/\/\/\/
 Click to see the full signature
Reply to
Chris H

I went on an interview once and they didn't ask that. But, that was before C became common. About 1974.

However, I did get asked how many ways there were to clear a register to zero on a IBM 360. There are quite a few. :-)

--
ArarghMail804 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html
 Click to see the full signature
Reply to
ArarghMail804NOSPAM

You can trim that a bit:

a = a + b; b = a - b; a = a - b;

Paralleling the original using ^ as add/subtract without carry/borrow. Using genuine add and subtract means we count on C to ignore overflow.

Mel.

Reply to
Mel

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.