RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1071 | Replies: 23 | Thread Tools  Display Modes
Reply
Join Date: Jul 2008
Posts: 11
Reputation: pdk123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
pdk123 pdk123 is offline Offline
Newbie Poster

help

  #1  
Jul 12th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,687
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 32
Solved Threads: 529
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: help

  #2  
Jul 12th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: pdk123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
pdk123 pdk123 is offline Offline
Newbie Poster

Re: help

  #3  
Jul 12th, 2008
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..
Reply With Quote  
Join Date: Dec 2005
Posts: 4,215
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 26
Solved Threads: 493
Colleague
Salem's Avatar
Salem Salem is offline Offline
Industrious Poster

Re: help

  #4  
Jul 12th, 2008
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: pdk123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
pdk123 pdk123 is offline Offline
Newbie Poster

Re: help

  #5  
Jul 12th, 2008
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'
Reply With Quote  
Join Date: Dec 2005
Posts: 4,215
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 26
Solved Threads: 493
Colleague
Salem's Avatar
Salem Salem is offline Offline
Industrious Poster

Re: help

  #6  
Jul 12th, 2008
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?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: pdk123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
pdk123 pdk123 is offline Offline
Newbie Poster

Re: help

  #7  
Jul 12th, 2008
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..
Reply With Quote  
Join Date: Jul 2008
Posts: 9
Reputation: stewie griffin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stewie griffin stewie griffin is offline Offline
Newbie Poster

Re: help

  #8  
Jul 12th, 2008
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
Reply With Quote  
Join Date: Sep 2006
Posts: 298
Reputation: Colin Mac is on a distinguished road 
Rep Power: 3
Solved Threads: 20
Colin Mac Colin Mac is offline Offline
Posting Whiz in Training

Re: help

  #9  
Jul 12th, 2008
The only potential problem you can be sure about is that 'boolean' isn't a standard type.
Reply With Quote  
Join Date: Jul 2008
Posts: 11
Reputation: pdk123 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
pdk123 pdk123 is offline Offline
Newbie Poster

Re: help

  #10  
Jul 12th, 2008
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'
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:34 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC