943,774 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1925
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 12th, 2008
0

help

Expand Post »
following is a simple embedded c piece of code, can somebody tell me is there a problem in it ?
  1. boolean new(int a)
  2. {
  3. return !(a & 7);
  4. }
i am new to embedded programming. It will be great help, any pointers to this ...

thanks
pdk
Last edited by Narue; Jul 12th, 2008 at 9:44 am. Reason: Added code tags
Reputation Points: 10
Solved Threads: 0
Light Poster
pdk123 is offline Offline
46 posts
since Jul 2008
Jul 12th, 2008
1

Re: help

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 12th, 2008
0

Re: help

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..
Reputation Points: 10
Solved Threads: 0
Light Poster
pdk123 is offline Offline
46 posts
since Jul 2008
Jul 12th, 2008
0

Re: help

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 12th, 2008
0

Re: help

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'
Reputation Points: 10
Solved Threads: 0
Light Poster
pdk123 is offline Offline
46 posts
since Jul 2008
Jul 12th, 2008
0

Re: help

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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 12th, 2008
0

Re: help

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..
Reputation Points: 10
Solved Threads: 0
Light Poster
pdk123 is offline Offline
46 posts
since Jul 2008
Jul 12th, 2008
0

Re: help

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
Reputation Points: 64
Solved Threads: 1
Light Poster
stewie griffin is offline Offline
35 posts
since Jul 2008
Jul 12th, 2008
0

Re: help

The only potential problem you can be sure about is that 'boolean' isn't a standard type.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Jul 12th, 2008
0

Re: help

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'
Reputation Points: 10
Solved Threads: 0
Light Poster
pdk123 is offline Offline
46 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Return values from functions
Next Thread in C Forum Timeline: Read from data file at some specified location





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC