Computer programmers' habits in electronics

Dec 20, 2005 146 Replies

Well, I think when he said "reverses a string in place", he meant the former rather than the latter, and probably implied without creating a temporary copy.

Hell is more interesting, and useful, but not a nice place.

Dirk The Consensus:- The political party for the new millenium http://www.theconsensus.org

bool IsPowerOf2(unsigned val) { unsigned i=val, j=1; while (i >>= 1) j*=2; return (j == val); }

Best regards, Spehro Pefhany

"it\'s the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com

how about this, untested, uncompiled c++

void rev3( const char* str, int l = -1 ) { if( l == -1 ) l = strlen( str )-1;

if( l > 0 ) { char c = str[l]; str[l] = *str; *str = c; // swap rev3( str+1, l-2 ); } }

the nice thing about it is that one does not have to supply length, start or fin for initial invocation. Not that I think that recursion is appropriate for this problem.

In your example, the user would need to supply strlen( l ) - 1 for fin, for the initial call. And in my example, the user would only supply str.

A bonus question: how to swap two integer (or char) values without using a temporary variable? In any language? Without any function calls?

i

very nice story about using integer arithmetic.

In place. Try to do it so that you could say afterwards, that was a ncie solution.

i

You would get the job, yes, I mean you would pass this test. I must note, however, that you would get a compiler warning about assigning integer to a char, and also you do not need both c and d. The last return statement is also not needed. You are definitely in the top 90% of computer programmers, no kidding.

i

I guess your fingers did the typing "const char *" without thinking. :)

i

I did the same thing, rotflmao!

i

A perfect answer! You get an A+!

i

:) Nasty things, those non-const pointers.

I meant naked pointers in general.

I suppose you should also ding me for .

It's a valid answer, and meaningful of itself. The follow up questions would bring it back to where we started.

If you're not tired of the game yet, write:

// return true if val is an integer power of 2; false otherwise. bool IsPowerOf2(unsigned val);

Gak! :) Yeah, different context.

#include

void reverse(char string[]) { char c; int i,j;

for (i=0, j=strlen(string)-1; i < j; i++,j--) c = string[i], string[i] = string[j], string[j]=c; }

What does it pay? When do I start?

Umm... I think you missed the implied smiley (;0~)

Bob

"Mike Young" wrote in news:kaqqf.2184$ snipped-for-privacy@newssvr11.news.prodigy.com:

Can I do it in Ada? I can do C++, but I'm not too fond of it. Note that Ada doesn't have an unsigned type, so I'm using an integer instead. The code would be the same regardless.

/

formatting link

function IsPowerOf2(val: integer) return boolean is result: integer; begin result := val rem 2; if result = 0 then return true; else return false; end if; end IsPowerOf2;

Puckdropper

www.uncreativelabs.net Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had. To email me directly, send a message to puckdropper (at) fastmail.fm

great job!

I wish that people who we see could do as good job as you did.

i

here's my try, could be a little faster, untested

bool IsPowerOf2(unsigned int val) { int count = 0; while( val ) { if( val & 0x1 ) { count++; if( count > 1 ) return false; } val = val >> 1; } return cunt == 1; }

Cool!

I recall vaguely that Ada lets you define ranges? Something like type uint32 is integer[0..4billionsumthing]? In any case, integer would be less restrictive. Is this an advantage or disadvantage in the application?

That returns true for even numbers. (I think. Is rem the modulus operator? returning the remainder of division by 2?) I was looking for true if val is

2 raised to some integer power, 2^n, where n is a positive integer. How would you write that in Ada?

Also, this seems a good time to discuss the read-mostly intent of Ada's more verbose coding style, versus the (alleged) "write-only" brevity of some other languages. For example, I would likely write the above function in C++ as a one liner:

bool IsEven(unsigned val) { return 0 == val % 2; }

Bit-operation equivalence aside, what are your thoughts on the readability of both versions, in context of both a large application and just this one simple function in isolation?

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required