A quick c programming survey question

You'd really write code like that? I dislike having computers shout at me. All caps macros may be traditional in C programming, but so are missing declarations defaulting to "int", lack of comments, and "if (x = 1)" bugs.

Sometimes it is useful to write individual macros like these (especially when you are not sure of the polarities of signals before testing), but I think it is a help to consistency if you use parenthesis on all macros:

#define ledOn() cbit(ledBit, PORTB)

That way you can more easily change things in the future, such as changing a macro definition to an inline function call (I mostly prefer "static inline" functions to macros, if the compiler handles them well).

And put parenthesis around "bit" in your bit macros, for safety (that's one of the reasons I prefer static inline functions).

Reply to
David Brown
Loading thread data ...

Access to particular registers or hardware is non-portable, and you should know the size or efficiency of the code your compiler generates for particular constructs - at least on small micros. In other words, bit-fields are not all that much worse than other methods on some compiler-target combinations, and are therefore worth considering.

Reply to
David Brown

Not necessarily. For example, a standard UART may be attached to any number of processors. The driver code for that UART can be independent of processor/compiler.

Even with today's highly integrated micro-controllers, "good" vendors frequently use the same peripherals in differnt mixtures for variants. The difference will often be nothing more than the base address of the registers.

When squeezed for space/time, all bets are off. However, to paraphrase, "first get it right, then optimize it." Too often I find embedded guys worried about optimizing that which needs no optimizing.

Using bit fields in hardware interfaces that need to be portable across compiler/processor targets, is asking for trouble. Besides, many hardware registers do not have simple access semantics, and are therefore not suitable for the read-modify-write operations generated by compilers for bit-fields. For example, read-only registers, and write-only registers.

--
Michael N. Moran           (h) 770 516 7918
5009 Old Field Ct.         (c) 678 521 5460
Kennesaw, GA, USA 30144    http://mnmoran.org

"... abstractions save us time working, but they don't
  save us time learning."
Joel Spolsky, The Law of Leaky Abstractions

The Beatles were wrong: 1 & 1 & 1 is 1
Reply to
Michael N. Moran

A good example of this is the UART used in the eZ80 series, which is identical in its interface to the 16450 types.

And registers which do something when you read them... it's quite common, for example, for the accessing of an interrupt status register to acknowledge the interrupt.

I think there is a case for a type qualifier (say "hardware") to be implemented, which allows the compiler writer to implement the dirty kludgy bits in the privacy of his own code, leaving the user with a pristine readable source. It's already commonly done with interrupt service routines, for example.

Paul Burke

Reply to
Paul Burke

Clifford Heath wrote in news: snipped-for-privacy@excalibur.osa.com.au:

Agreed - I've seen bitfields for "left" and "right" in a joystick interface swap position when changing the optimization level. The compiler writers justifiably pointed to the ANSI spec showing they could do what they thought appropriate.

They later forced the compiler to use "intuitive" ordering, but that was lesson enough for me to never use bitfields again for accessing sections of a hardware register.

One other advantage of #defined constants is that you can combine separate logical groups and read or write them with a single operation, spurious example:

response = getBits( readChannel, RCIN | RCERR );

Of course, we never use RCIN + RCERR, do we?

Peter.

Reply to
CodeSprite

ideal

If youb haven't now seen the answer ask again

tim

Reply to
tim

But Misra takes rules just a bit too far (and then some more)

tim

Reply to
tim

Indeed.

But I was pointing out that PCLint can be tailored to complain about almost anything you don't like. The warning about using commas outside of a 'for' statement is an optional warning that can be individually switched on.

It is an excellent tool for enforcing a coding standard.

Andy

Reply to
Andy Sinclair

What was their "correct" answer.

I've seen both round and square ones, so the question sounds dumb to me.

--
Burn the land and boil the sea,
 You can't take the sky from me.
Reply to
Geoff McCaughan

They can't fall or be pushed into the manhole itself... square ones can.

Well, it's an unusual question.

Casey

Reply to
Casey

That is the correct answer but I disagree that it is a Microsoft interview question. M$ interviews are all day affairs and you are indeed presented with questions designed to provoke thought, but they do not have just one "correct" answer. They will ask you questions like "How many gas stations are there in the USA?" or "How much water flows out of the Mississippi River into the Gulf of Mexico each day?" They are wanting to see if you can make an intelligent guess within an order of magnitude.

Read Jon Bentely's Programming Pearls or Expert C Programming: Deep C Secrets by Peter Van Der Linden for more on these types of questions.

Reply to
Bruce

Round covers can not fall into the manhole since the hole is smaller than the cover. Gary K8IZ Washington State Resident Registered Linux User # 312991

Reply to
Gary P. Fiber

But that is not necessarily the only reason. Round manhole covers are easier to maneuver since you can roll them out the way, and you don't have to line them up exactly when replacing the covers.

--
Darin Johnson
    "Look here.  There's a crop circle in my ficus!"  -- The Tick
Reply to
Darin Johnson

OK I can buy this as a question requiring 'general' knowledge. What do I need to know. US population 200+ million, about half of them have cars. How many cars visit a specific gas station each day, I can probably work this out from experience. Divide one into the other, multiply by frequency of visits to get a 'reasonable' answer.

Um,

How wide is the river, 400 yards, a mile, 2 miles: really have no idea. How deep is the river 10 feet, 20 feet, 30, 40: even less idea How fast does it flow, don't even know where to start.

2 miles an hour, 4, 20?

Nope, one reasonable guess could be 300 times different to another one?

Easy peesy (perhaps) for someone who's been there often (I've been once), but not for someone who hasn't, and it's possible that a resident of WA will have never been

tim

Reply to
tim

Actually closer to 275 million.

Right, you could get a reasonable answer. But don't make any assumptions. Ask what research tools you have at your disposal. Can you use the internet, the phone book, whatever.

Well, perhaps I assume too much but I thought Huck Finn was mandatory reading and he talks about it being a mile wide. As for depth, someone I would hire would surely know that it couldn't handle all the commerce it does at 10' deep or even 20'. And if you think it flows 20mph, I don't want to hire you either. Surely you've seen big rivers before and none flow that fast.

That all said, your extreme answers are really not too far apart.

One I used to like, which is really easier but a nice warmup, ask the candidate how many basketballs would it take to fill the room you're interviewing in. You're looking for the idea that a bball occupies pretty close to a cubic foot when stacked. You're looking at how they estimate the dimensions of the room. Clever candidates will ask if they can step it off or count floor tiles or ceiling tiles. Do they subtract a factor for the furniture? You get the idea.

Reply to
Bruce

interview

presented

one

stations

it

You know, as an embedded SW engineer, I would be much happier working with someone who would get their behind out of the chair and do their own measurements! As opposed to waiting for someone else to wire up the logic analyzer for them. :-)

Reply to
Anthony Wong

I'm with you but the point is, can they THINK? If the answer is yes, anything else can be learned, including logic analyzers, emulators, scopes, languages, new microcontrollers, etc. This business is about change.

Reply to
Bruce

Here I would base my answer on a couple of small towns I know, of roughly 200 and 750 population, with 1 and 3 stations respectively. This works out to 1 per 250 population. The US population is closer to 300 million than 200, so I would extrapolate around 1 million. I doubt this is off by more than a factor of 3.

I agree. Needs something for a basis. We can assume a width and depth, getting a cross-sectional area, and assume some sort of velocity in the order of 1 to 10 mph, and go from there. I would try 1 mile wide and 100 feet deep. That would allow a figure in cubic feet per minute. Every component value has about an order of magnitude uncertainty. That works out to about 30 million cfm,

*60*24 = 7e10 cf/day. +-3 in the exponent value!!

My answers above illustrate something I deem important - an estimate of the accuracy.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

The part I waded across was bout 20 feet wide and 8 inches deep. :)

Completely off topic, but I would have guessed lower by maybe one order of magnatude. For most of the Mississippi's length, the Army Corps of Engineers has to dredge to maintain a 9 foot deep channel. "Mark Twain" was the term for a depth of two fathoms (12 feet). That was considered the minimum safe operating depth for Riverboats of the 19th century.

Parts of the lower Miss (fed by the Ohio) are deeper, but even the deepest shipping channels in the lower Miss delta area maintained by the Corps are on the order of 40-50 feet.

The current is around 3-4 mph.

According to

formatting link

The flow rate at New Orleans is 600,000 cfs. Thats 36 million cfm. You were well within 20%!

My answer would have been quite a bit lower. As a resident of the upper Mississippi, I underestimated the flow from the Ohio. IIRC, the Ohio is actually the bigger of the two rivers, and by normal geographical conventions, the lower Miss should be called the Ohio river.

--
Grant Edwards                   grante             Yow!  But was he mature
                                  at               enough last night at the
                               visi.com            lesbian masquerade?
Reply to
Grant Edwards

... snip ...

... snip ...

In a way it is topical. You need up front means of computing resources needed for a given application. That way you don't even start on a hopeless project, or you investigate the bottleneck first.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

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.