LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2007, 07:27 AM   #16
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled

Quote:
Originally Posted by atulsvasu
The possible confusion is because my search is not clear for you.
in 16-bit case, I check if the number is greater than 2^8 or not,
if yes, I rightshift by 8, Then I check if it is greater than 2^4 or
not, if yes shift 4, then if it is greater than 2^2 shift 2, then if
greater than 2^1 shift 1. Whenever I shift the number by somevalue I
increment the output by same quantity. It is almost a binary search,
you may wonder if comparison takes multiple cycles, it may but
machine can ensure even that is O(P(log(n))), if n is very large, moreover
it is not our concern. Our concern must be number of instructions.
Your code makes sense now, but it only checks for the highest-set bit and none other. As you stated, this will be very efficient for a high number, but here you essentially log2 the cycles but adds an additional 2 operations per cycle, and it must be accomplished three times to allow for second and possible third bits so it uses a max of 48 basic math operations (not including comparisons,) whereas checking one at a time can result in at most 16. The actual complexity of yours seems to be O(x log n) where x is the number of elements in the combination. Take this vs. O(n) and yours comes out ahead for very large numbers or low numbers of combined bits, but not with 16 bits. It looks like yours would come out ahead at 75 bits and above based strictly on number of arithmetic/assignment operations.
ta0kira
 
Old 07-05-2007, 07:06 PM   #17
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I follow the principle of never optimise unless you have to, and then when you do make it as specific as possible. I would make a few comments. First if possible write your code knowing the number of bits you number has, second if it makes sense avoid error checking, third do the smallest amount of work and then get out of there.

So with that in mind I would find the location of the highest bit, don't worry about any other bits or if there are more than two. I would also assume that the integer is a 16-bit number.

I would then use ta0kira's approach but reverse it, that is look for the largest bit first, additionally I'd use literals rather than constants. I would also remove the error checking and break out as soon as the condition has been met.

This would give something like:
Code:
int posn = 0;
if (val & 65536)
   posn = 16;
else if (val & 32768)
   posn = 15;
else if (val & 16384)
   posn = 14;
else if ...
Obviously you need to be sure that,
1) removing the error checking is acceptable, posn = 0 if there are no bits set.
2) all the bits are accounted for, adjusting the code appropriately

Metric of the algorithm, whilst big O is traditionally used to measure the efficiency of an algorithm, it only make sense when extrapolated to large numbers, here it is a small piece of data that is being examined and the fixed number of instructions per iteration do make an impact on the overall efficiency.

Last edited by graemef; 07-05-2007 at 07:09 PM.
 
Old 07-05-2007, 07:40 PM   #18
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Dang, just read the OP again and spotted the comment
Code:
/* run another loop to find the second value, check that there are no more values *
So you need both bits, in which case I would suggest.
Repeat the code this time looking from the smallest bit.
Code:
int posn_low = 0;
if (val & 1)
   posn_low = 1;
else if (val & 2)
   posn_low = 2;
else if (val & 4)
   posn_low = 3;
else if ...
Finally if you want to check that there are no other bits set then, you can use something like:
Code:
if (val != ((1<<posn_high) + (1<<posn_low)))
/*error situation */
and also something like
Code:
if (posn_high == posn_low)
/*error situation */
You do have quite a few lines of code, but the number of instructions executed is optimal (I think)

Last edited by graemef; 07-05-2007 at 07:44 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
installing gcc breaks binary mimithebrain Ubuntu 1 03-27-2006 12:10 PM
Looking for a GCC binary for Turbo Linux 10F Thaidog Linux - General 0 07-23-2005 09:42 PM
need binary gcc phoenix_wolf Linux - Software 1 12-02-2004 03:16 AM
Installing gcc-where to get binary copy mjkramer Linux - Newbie 5 10-12-2003 10:10 PM
creating a flat binary file with gcc wsimmons Programming 2 01-08-2002 11:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration