There are 127 messages in this thread.
You are currently looking at messages 90 to 100.
Vladimir Vassilevsky wrote:
>>> Ask to write code to toggle a variable from 0->1->0...
>> Which is a correct and proper way to do this.
>> if (a) a = 0;
>> else a = 1;
>
> No, it is not.
>
> if(a != 0)
> {
> a = 0;
> }
> else
> {
> a = 1;
> }
> MISRA Rulez.
MISRA is good, but sometimes way too restrictive for my taste.
> Alternative acceptable realization:
>
> template <class T> toggle(T& a);
Better. Since it may be plain C, simple
a = toggle_0_1(a);
would do. :)
>>> Or ask them how to test a bit in a peripheral register.
>>
>>
>> (tst_bit(register, 3)
>
> Dirty hack.
> There is no such things as bits and registers.
On the application level, of course not, but that was not the question.
> if(something_happened())
> {
> }
Well, someone has to implement "something_happened()" function. :)
--
WBR, Yuriy.
"Resistance is futile"
"Yuriy K." wrote: > Rainer Buchty wrote: > ... snip ... > >> So if the *programmer* knows that "a" is either 0 or 1, why >> shouldn't he write "a^=1;" (or "a^=SOME_MASK;" for the arbitrary >> case) right from the beginning but rather if/then? > > Because three years later new programmer will spend two hours > trying to figure out if *a* can or can not be equal to 2 or 3, and > what is this silly line meant to do. Which is handled concisely by the (elsethread) suggestion: a = !a; Other possibilities include using the C99 declaration #include <stdbool.h> bool a; or using Pascal a : boolean; -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net>
Arlet wrote: > If it doesn't need to be optimized, I prefer this version: > > a = !a; That has the advantage of still working even if "a = 2". -- Darin Johnson
>> Any other ideas for interview questions?
>
>
> Ask to write code to toggle a variable from 0->1->0...
>
> We often saw this:
>
> if ( a == 0 ){ a=1;} else {a = 0;}
>
> instead of e.g
> a = 1-a; or a^= 1;
>
VAR flag: Boolean;
flag = not flag;
or did you have to write in C ;-)
--
Best Regards,
Ulf Samuelsson
u...@a-t-m-e-l.com
This message is intended to be my own personal view and it
may or may not be shared by my employer Atmel Nordic AB
Ulf Samuelsson wrote:
>>>Any other ideas for interview questions?
>>
>>
>>Ask to write code to toggle a variable from 0->1->0...
>>
>>We often saw this:
>>
>>if ( a == 0 ){ a=1;} else {a = 0;}
>>
>>instead of e.g
>>a = 1-a; or a^= 1;
>>
>
>
> VAR flag: Boolean;
>
> flag = not flag;
>
> or did you have to write in C ;-)
Of course not.
and equally valid answers to the question are
TX BIT P0.4
CPL TX
or
PIN = Flag;
Flag.d = !Flag; /* toggles on every CLK */
-jg
Ulf Samuelsson wrote:
>
>>> Any other ideas for interview questions?
>>
>> Ask to write code to toggle a variable from 0->1->0...
>>
>> We often saw this:
>>
>> if ( a == 0 ){ a=1;} else {a = 0;}
>>
>> instead of e.g
>> a = 1-a; or a^= 1;
>
> VAR flag: Boolean;
>
> flag = not flag;
:= :-)
>
> or did you have to write in C ;-)
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
CBFalconer wrote:
> Ulf Samuelsson wrote:
>
>>>>Any other ideas for interview questions?
>>>
>>>Ask to write code to toggle a variable from 0->1->0...
>>>
>>>We often saw this:
>>>
>>>if ( a == 0 ){ a=1;} else {a = 0;}
>>>
>>>instead of e.g
>>>a = 1-a; or a^= 1;
>>
>> VAR flag: Boolean;
>>
>> flag = not flag;
>
>
> := :-)
Does that mean Ulf would have failed the interview ? :)
The questioner could have required this line :
[Will compile in both Pascal and Modula-2 ]
flag := NOT flag;
-jg
On Thu, 16 Nov 2006 12:22:55 -0600, "Yuriy K." <y...@mail.ru> wrote: >> Next question: Compute the square root of an integer. >> Yuriy's answer: s = squareroot(a); > >Actually, it is >s = sqrt(a); >C programmer shall know basic standard library functions. Sorry, failed. Question was: "...squart root of an integer" Your solution imposes two castings and pulls in floating point arithmetics. -- 42Bastian Do not email to b...@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
On Thu, 16 Nov 2006 12:28:56 -0600, "Yuriy K." <y...@mail.ru> wrote: > >Because three years later new programmer will spend two hours trying to >figure out if *a* can or can not be equal to 2 or 3, and what is this >silly line meant to do. I expect a "new programmer" to understand the whole source he is responsible for and not only one line of code. Most code (C,Assembler, Cobol ....) is hard to understand without the context. > >More often than not it is more important than saving two bytes and 500 >nanoseconds. It is about embedded where 500ns could mean "go" or "no go". -- 42Bastian Do not email to b...@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
42Bastian Schick wrote: > >Actually, it is > >s = sqrt(a); > >C programmer shall know basic standard library functions. > > Sorry, failed. Question was: "...squart root of an integer" > Your solution imposes two castings and pulls in floating point > arithmetics. But his solution does produce the square root of an integer, as requested. The solution is simple, and it works (although you could complain the result is truncated rather than properly rounded). It could also allow a smart compiler to recognize it can pull in an optimized version of integer square root. If you wanted it optimized, you should have provided more info. What is the word length, target CPU, required precision, and size/space constraints ? Do you want it in generic C, or ASM ?