Computer programmers' habits in electronics

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?

Reply to
Mike Young
Loading thread data ...

Misspelling on a whiteboard can be overlooked without comment. Some are just more unfortunate than others.

Hand optimization isn't too very important. What is important is correctness. Does it return true for all and only integer powers of 2? Does the loop terminate? One boundary condition to test is val = 0. (Is that a power of 2?) What changes would you make if val is changed to int rather than unsigned?

Also in context of optimization, what if I then mumbled something about IEEE floating point representation? Assuming the target system is relatively efficient with floating point values, say a modern Intel box, what thoughts come to mind?

Reply to
Mike Young

Stolen and revised from "The C Programming Language" Second Edition, Brian W. Kernighan & Dennis M. Ritchie, Prentice Hall Software Series, Pg 63.

In the text, the c variable is an integer and string[] is s[]. They can be forgiven, I think. The legacy of C is its occasional terseness.

Every time I see interview questions like these I am amused. First, because a solution to a question like these is as much a test of the interviewer as the interviewee, since no one can spot plagiarism in code very easily. Can code be plagiarized? What happens when a programmer uses techniques or styles or generalized solutions to the same problems from one project to the next or from one job to the next? I suppose the real test would be for the interviewee to be able to come up with this code from memory. I know I couldn't do it anymore, but I am an old fart and not a young whipper-snapper with a fresh degree. I remembered it from reading the book but I had to dust it off and crack it open to find it.

Solutions to programming problems ought to be the equivalent of putting tinker toys together by now. It is, after all, the 21st century and software design is almost 50 years old. Libraries exist that can solve these problems far more reliably than reinvention every time a project needs to be done. These questions only serve to break the ice or simulate a development environment where the give and take between the team members can advance the project or set it back. It's up to the team members to determine those qualities that are desirable in their candidates.

A truly valuable programmer is one who has a whole tool chest full of source libraries that can be applied to projects with little or no modification. The equivalent of LSI chips, if you will, connected to the other functions (chips) with a little glue logic to create a system.

Q. "Why are manhole covers round?" A. "To keep them from falling in."

Bzzzt! "Because manholes are round."

Reply to
Geoff

Ah, very good!

But I think to take care of all cases you might have to do:

return val?!(val&(val-1)):0;

Scrunched together, especially, it will also better confuse anyone who comes along later and tries to modify "your" code.

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
Reply to
Spehro Pefhany

"Mike Young" wrote in news:cytqf.40824$ snipped-for-privacy@newssvr27.news.prodigy.net:

*snip*

It's both... It's a major advantage in that I don't have to mess with anything extra, it's also a disadvantage in that what happens with negative results (say from an integer overflow) is undefined. I should have used "Natural" instead.

You can define other types, but that caused trouble with some of the type casting I had to do. (My solution involved using a log which Ada only seems to have defined as float.)

Anyway, to emulate a unsigned int on my machine at least, you'd do: type unsigned is new long_long_integer range 0..4294967295;

*snip code sement *

Not exactly... rem and mod are equal for positive numbers, but different for negative values. (Close enough in this case.)

My mistake. I misunderstood the question. I get some mathematic terms messed up at times. Here's the code that does as you wish:

function IsPowerOf2(val: integer) return boolean is subtotal: float; result: integer; result2: integer; begin subtotal := log(float(val), 2.0); result := integer(subtotal); result2 := 2**result; return val = result2; end IsPowerOf2;

Basically what I'm doing is taking the log of a number, raising it to the base again and comparing results. If there's no floating point part, then the numbers should be equal.

I like "write-fast" rather than "write-only." You can make C-based code as readable as read-mostly languages, it just takes care.

function IsEven(val: natural) is begin return 0 = val rem 2; end IsEven;

As implied above, any language's code (well, except purposely obfuscated ones such as White Space (-;) can be written so as to be very readable. IMO, the begin and end in Ada makes reading (especially for debugging) nested code much easier because you don't lose yourself in a fluery of end braces - }.

I also feel that Ada demands good code while C++ is just happy to have something to compile. In C++, assigning a float to an integer is as simple as float = integer while in Ada you have to explicity cast it: float := integer(integer)

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
Reply to
Puckdropper

Naughty!

Yes.

Remember that the intervieweee sits in a room with no internet access.

Sounds like a good thing, assuming that they are good solutions.

I am not quite sure what you are saying here.

If an intervieweee passes my test because he actually remembers a sample from K&R, all the power to him.

You see, in our practical experience, tasks somewhat similar to in place string reversal come up frequently in real life.

This is a test to separate BSers from real programmers.

Put it more bluntly, a lot of people say that they are programmers in resumes, whereas in fact they are not. This test is designed to filter those people out. No one is perfect and we would accept some mistakes, as long as it looks like it is written by a real programmer.

Let me give you a poor analogy.

Suppose that you interview someone claiming to be a circuit designer. The caldidate is charming, read AoE, read this newsgroup for a while, and is in fact pretty good and BSing and "active listening", and other psychology tricks. You sit there and think, what a pleasant and knowledgeable guy, what a pleasure to talk to.

Without a little test, you (the general you) may easily become confused and disoriented by someone who is pretty good at bullshitting.

Same here, the test is not the whole interview, not the end all, but a good filter.

i

Reply to
Ignoramus16420

Ig,

Perhaps it really is poor practice, but programmers just get away with it (because the cost is "softer")?

Consider...

  • If it cost you many more hours to write (assemble) each hack
  • And it cost you money for each compile (build with parts, PCB)
  • And flaws subtly delete (burn up) parts of your project when tested
  • And it took you serious time (and real money) to find and re-code (re-solder) the missing parts from scratch

Would hacking code still seem like a smart approach?

When hacking was more costly (e.g., timeshare systems and punchcard programming), a structured approach was valued. It's just less painful now, so it's more common.

The same goes for resources. Today we have abundant processors / networks / memory / disk - this doesn't improve the quality of lousy code, it only hides it. Sadly, fast time-to-market is often rewarded more than quality.

Richard

Reply to
Richard H.

Actually, doing things in-place like the reversing of string example is quite common and useful in embedded code. Especially for small 8/16 bit micros where memory is very scarce (Yes RAM is cheap but if you can use the on-chip memory that saves the cost of one chip. And in some applications $0.10 is considered expensive).

Reply to
slebetman

How many times have you had to write a routine like this in your coding career? I'll answer: only when asked to by some clown on an interview.

I've had people ask me to reverse a fifo. Most coders try to avoid reversing fifos...

Another guy asked me the fastest way to reverse bits in a word. He didn't like the table lookup answer.

When I got out of college, an interviewer from Microsoft asked me if I could figure out a way to create a doubly linked list using a single pointer field per element. (hint: if A xor B = C, then A xor C = B). No wonder their software is so horrible. They hire people who would actually consider doing this.

I've always found it enjoyable to torture prospective employees, but it never helps in determining if they are going to be productive. Often, the smartest people get nothing done, and even become a drag on the organization by bullying and posturing in meetings. It's the quiet, less 'clever' people with the good work ethic that end up doing most of the work.

--
Regards,
  Bob Monsen

The concept of fiction was nowhere in Mike\'s experience; there was
nothing on which it could rest, and Jubal\'s attempts to explain the
idea were so emotionally upsetting to Mike that Jill was afraid that
he was about to roll up into a ball and withdraw himself.
Reply to
Bob Monsen

Doing things of similar nature, many times.

I think that it would be stupid to hire people based only on being able to write a little routine. We agree on that. But it would be stupid to hire people who cannot program computers, to program computers.

i
Reply to
Ignoramus16420

The first version of BASIC for the (long defunct) Commodore Amiga computer was provided by Microsoft. They used 32 bit words to keep track of addresses (pointers) but, being 'clever,' used the uppermost byte for various flags since (I believe this is the correct reason, someone correct me if I'm wrong) the original Amiga 1000 used a Motorola 68000 that could only address 16MB (24 bits) anyway.

This came back to bite them when the Amiga 2000 was released with 68020, which could address over 16MB.

Oops!

On the other hand, it can be a non-trivial choice as to how to optimize a design -- PCs are so fast and have so much memory these days that no one should think they're being clever by packing bits into the top-end of a DWORD or building doubly-linked lists as you describe, yet people working on embedded systems with little 8 bit CPUs can arguably make good use of such techniques.

Hmm... thinking about it, I'd admit to not being able to implement a doubly-linked list using a single pointer field. Say the addresses and (pointer) contents of of element are:

0x0123: Magic number 1 0x0345: Magic number 2 0x0678: Magic number 3 0x09ab: Magic number 4 ...etc...

I don't see how any single magic number (16 bits) that you place at 0x0345 that'll let you navigate to both 0x0123 and 0x0678?

---Joel

Reply to
Joel Kolstad

Asking that string reversal problem isn't, in my opinion, such a bad thing. There's a difference between someone who just can't code and someone who hasn't coded on a particular processor's assembly language. If I care about what processors someone has worked on it is only to get a feel for how flexible they are -- someone who's only done

8-bitters may be weak at handling really large and complex problems, while someone who's done only C++ on 32 bit platforms is going to get some rude surprises working on an AVR.

In addition to The Interview Question I related above, I also like to ask an interviewee to give a quick chalk talk on some project that they've worked on. This quickly gives you a good idea of their ability to communicate, where they are going to want to fit into the hierarchy, how much stuff _they_ actually did personally, and how good of a grasp they had on the principals.

I should also mention that in the company where they used The Interview Question and the chalk talk, we also made sure that every software engineer talked to at least one EE, and we made sure that part of that discussion was a test of their hardware skills -- the EE would stick a schematic under their nose and ask them to find a signal coming out of the microprocessor, and ask them to relate how they'd measure the signal. Saying "I don't know but I do know how to ask an EE for help" was acceptable; saying "I don't do hardware" was not.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com
Reply to
Tim Wescott

No, but it's possible under less limiting conditions. Described here (spoiler):

formatting link

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
Reply to
Spehro Pefhany

Depends. Some people learn fast. I have often come across job requirements for knowledge of a particular 8 bit micro. I used to say that it would take me about half a day to get up to speed by reading the data book. Not good enough. Now I bullshit, and if I get the job spend half a day getting up to speed...

Ditto programming languages. Originally it took me a couple of weeks to learn to program reasonably well in C, then some years later another two weeks to add the

++. Then I didn't use it for about 4 yrs and so have had to relearn again for my current job. Not a hope in hell of getting the job if the guy had set me some poxy C++ test at the 'interview'.

Hiring a coding monkey over someone who can actually understand the entire project, including h/w design, is IMO false economy. My job is solving engineering problems.

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Reply to
Dirk Bruere at Neopax

How well does that fly? Have you come across any disbelievers?

Reply to
Mike Young

Assume you have a circular list of A, B, C, D, A

Then A's link field has B^D, B's link field has A^C, etc. Now, given the address of two consecutive pointers in the list, p and q, you can go either way, since

next = p ^ q.link

and

prior = p.link ^ q

This does not give you what you usually want from a doubly-linked list, though, which is the ability to remove an element from a list given only a pointer to the element.

--
Regards,
  Bob Monsen

Jill looked puzzled. "I don\'t know how to express it. Yes, I do! Ben,
have you ever seen an angel?" "You, cherub. Otherwise not." "Well,
neither have I but that is what he looked like. He had old, wise eyes
in a completely placid face, a face of unearthly innocence." She
shivered.
Reply to
Bob Monsen

Not amongst my customers. Obviously I tend to work for smallish companies without all that formal HR crap. They prefer Jack Of All Trades who can understand the whole project in all its phases from idea to production. The fact I spend an extra couple of weeks learning specifics is of far less importance than being able to pick up h/w, s/w, standards, EMI and productionisation problems before it's too late.

I have worked for big companies, and the compartmentalised mindset is vastly counterproductive. Sure the coding monkeys can write faster than me, but when it doesn't work and the h/w and s/w guys are blaming each other that's not much of an advantage.

The last small company I worked for had a team of 4 of us in R&D. We could get a product from idea to mass production and in the customer's hand in 6 weeks.

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Reply to
Dirk Bruere at Neopax

My CV

formatting link

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Reply to
Dirk Bruere at Neopax

Most "modern" programming languages deserve the title "code"; they must be painstakingly deceiphered to try to figure out what's going on. The proper documentation of code is comments, which are rare these days.

John

Reply to
John Larkin

job

to

the

my

I think that the test I am asking for does not require knowledge of C beyond the very basics. It basically requires knowledge of computer programming rather than knowing intricacies of the language.

i
Reply to
Ignoramus14405

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.