954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help

following is a simple embedded c piece of code, can somebody tell me is there a problem in it ?

boolean new(int a)
{
      return !(a & 7);
}

i am new to embedded programming. It will be great help, any pointers to this ...

thanks
pdk

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

>can somebody tell me is there a problem in it ?
Assuming boolean is a typedef, there's nothing syntactically or semantically wrong with that function. Perhaps if you told us what it's supposed to do, we can tell you if it actually does it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

it was asked for me in the test. where they asked me
1.what the code does and
2. the potential problem with that.

i answered, If any of the last 3 lsb bits of the number is set to '1' return '0'. Else return 1. and i could not see any potential problem in that..

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Your analysis is correct.

But whether there is anything 'wrong' depends on what it's supposed to do.
If for example is was a test to see if a number was >= 8, then it would fail miserably on negative numbers.

The name 'new' certainly doesn't help.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

thankyou for the reply.
but i am not sure whether this logic will work ( i.e to check whether the number is >=8) .. as lets say for eg +9 , the program still returns '0'

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

True.
So what's the indented function then?
If we don't know what it's supposed to do, how can we say whether it's right or wrong?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

even i was not sure. because in the test, i was supposed to tell what the function is intended to do and any potential problems !!!.
i could not exactly figure out this .. i.e why any pointers to this will be helpful.

as pointed out, i was also thinking whether it will cause any problems for -ve numbers .. but without any potential soln..

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This function resave number, if the number is 0 its return true else its return false.
The Boolean " a & 7 ;" says if a != 0 and 7 != 0 then 1 else 0
Has you see the 7 is not needed

stewie griffin
Light Poster
35 posts since Jul 2008
Reputation Points: 64
Solved Threads: 1
 

The only potential problem you can be sure about is that 'boolean' isn't a standard type.

Colin Mac
Posting Whiz
327 posts since Sep 2006
Reputation Points: 78
Solved Threads: 22
 

This function resave number, if the number is 0 its return true else its return false.
The Boolean " a & 7 ;" says if a != 0 and 7 != 0 then 1 else 0
Has you see the 7 is not needed

--------------------------------

when the number is lets say '8'. it still returns '1' .. i.e basically numbers for which lsb 3 bits are not set. it will return '1'

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

So knowing what it does, you can write a spec which means there isn't a problem, or you can write a different spec where there is a problem.

I suppose pedantically, the parameter should be unsigned, if you're messing around with bits.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Hmmmm that was good question, it need a 2 min think before we could answer that question. I never an interview question like that before. But that was good one.

I don't see any improvements when we could do to that code. Everything seem to be good. And I do think 7 needs to be there if you wanted check the last 3 bit of a to check if they are set.

And again it all depends upon the requirement!

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

My guess:
1)The function checks whether the integer a is divisible by 8.
2)The use of new which is not an operator in c but in c++.

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

yes, this looks like the correct answer.

I was thinking that for -ve numbers this may fail. But this even checks for -ve numbers stored in 2's complenent form correctly i guess.

Then i donot see any potential problem in the function. except that function returns 1 for '0' also and all numbers (-ve and +ve ) divisible by 8.

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

now i got some part of this answer:
i have couple of other questions in the same form.

which i gave some answers, May be u can correct me , if i was wrong:

1 bool f( uint n )

{

return (n & (n-1)) == 0;

}

My answer was : if n is powers of 2 then return 1 else '0'

2. uint f( uint n )

{

return –n & 7;

}

My answer: last 3 (LSB) bits of the 2's complement of the input number .

3. int f( int n, int l, int r )

{

return (n << l) >> r;

}

my Answer : n is bit shifted left by l bits and result is again right shifted by n bits.
6. void fn(long* p1, long* p2)

{

register int x = *p1;

register int y = *p2;

x ^= y;

y ^= x;

x ^= y;

*p1 = x;

*p2 = y;

}

my answer : swaps the two inputs x and y.

7. void send(int count, short *to, short *from)

{

/* count > 0 assumed */

register n = (count + 7) / 8;

switch (count % 8)

{

case 0: do { *to = *from++;

case 7: *to = *from++;

case 6: *to = *from++;

case 5: *to = *from++;

case 4: *to = *from++;

case 3: *to = *from++;

case 2: *to = *from++;

case 1: *to = *from++;

} while (--n > 0);

}

}

My answer : this expression copies the counth element of 'from' array to the first element of the 'to' array.

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

How about using code tags?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

how do i use it ?

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
how do i use it ?

see here

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

It would certainly fail on -ve numbers which didn't use 2-s complement. Nothing in the C standard prevents other ways of storing integers .

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

yes, i.e TRUE, for other representations of -ve numbers it will fail.

pdk123
Light Poster
46 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You