Hi All
I have written a program to count the maximum contiguous set bits in an
integer .
Like if my binary representation of integer is :
1100111 : then output should be 3.
111000111110000101010111111 : then output should be 6.
I am including the snippet below.
How can I optimize this code and also is there a one liner to
implement the same.
(Like for power of 2 we have got (number & (number -1))).
Here is the program:
int main()
{
int count=0,n,i,temp=0;
printf("Enter the number \n");
/*Numer is in decimal for calculation we have to use binary
representation for calculation*/
scanf("%d",&n);
for (i=0;i<(8*sizeof(int));i++)
{
if((n&1)==1)
count++;
else
{
if (temp<count)
temp=count;
count=0;
}
n= n>>1;
}
if (temp==0)
printf("count of contiguous bits= %d\n",count);
else
printf("count of contiguous bits = %d\n",temp);
return 0;
} 3 3347
Vish wrote: I have written a program to count the maximum contiguous set bits in an integer. Like if my binary representation of integer is : 1100111 : then output should be 3. 111000111110000101010111111 : then output should be 6.
I am including the snippet below. How can I optimize this code and also is there a one liner to implement the same. (Like for power of 2 we have got (number & (number -1))).
How about a 5 liner?
int longest1BitsCount (unsigned long l) {
int i;
for (i=0; l; i++) l &= l + l;
return i;
}
Like any other program, I have no idea what this does on a 1s
complement machine (and don't really care).
---
Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/
On Fri, 29 Apr 2005 04:05:04 -0700, websnarf wrote: Vish wrote: I have written a program to count the maximum contiguous set bits in an integer. Like if my binary representation of integer is : 1100111 : then output should be 3. 111000111110000101010111111 : then output should be 6.
I am including the snippet below. How can I optimize this code and also is there a one liner to implement the same. (Like for power of 2 we have got (number & (number -1))). How about a 5 liner?
int longest1BitsCount (unsigned long l) { int i; for (i=0; l; i++) l &= l + l; return i; }
Clever piece of code. was it intended to be mildly obfuscated i.e. using
i and l which can look very similar, and using l+l instead of the clearer
l<<1?
Like any other program, I have no idea what this does on a 1s complement machine (and don't really care).
Since l has an unsigned type what signed integer representation an
implementation uses has no relevance to it.
Lawrence
Lawrence Kirby wrote: On Fri, 29 Apr 2005 04:05:04 -0700, websnarf wrote:
Vish wrote: I have written a program to count the maximum contiguous set bits in an integer. Like if my binary representation of integer is : 1100111 : then output should be 3. 111000111110000101010111111 : then output should be 6.
I am including the snippet below. How can I optimize this code and also is there a one liner to implement the same. (Like for power of 2 we have got (number & (number -1))).
How about a 5 liner?
int longest1BitsCount (unsigned long l) { int i; for (i=0; l; i++) l &= l + l; return i; }
Clever piece of code. was it intended to be mildly obfuscated i.e. using i and l which can look very similar, and using l+l instead of the clearer l<<1?
Shifts are slow on the Pentium 4 (and on Intel CPUs in general, versus
say an add, or how fast they are on AMD CPUs). It actually usually
doesn't matter, but I just default to the fastest path by instinct.
---
Paul Hsieh http://www.pobox.com/~qed http://bstring.sf.net/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: chirs |
last post by:
Hi,
What is the maximum number in JavaScript? I tried a large number like
0xff...ff with 30 fs, it still gives me a number, not an infinity. I
use IE6.
Thanks.
|
by: G Patel |
last post by:
Hi, I'm wondering if anyone knows if the following function will
function properly as a set-bit counter on non 2s complement machines
(as K&R2 implies).
| int bitcount(unsigned x)
| {
| ...
|
by: Mike |
last post by:
Howdie, I am a beginner, newbie.
I recently read some code that used masks and bits like
#define FL_VERBOSE 0x00000001
#define FL_MAX 0x00000002
....... etc ...
and then tested...
|
by: Digital Puer |
last post by:
I'd like to be able to create an integer from a range of contiguous
bits
in another integer. I'm on a big-ending machine, in case that matters.
I would ideally like to create a function with this...
|
by: yawnmoth |
last post by:
I'm trying to figure out how big integers in PHP can be and am having
some difficulty.
My first idea was to try something like this:
<?
for ($bits=0; (1<<($bits+1)) > (1<<$bits); $bits++);...
|
by: Martin Pöpping |
last post by:
Hello,
does a String in C# have a maximum length?
I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".
In every...
|
by: garyusenet |
last post by:
I'm trying to investigate the maximum size of different variable types.
I'm using INT as my starting variable for exploration. I know that the
maximum number that the int variable can take is:...
|
by: Mack |
last post by:
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
Thanks in advance,
-Mukesh
|
by: lovecreatesbea... |
last post by:
I read some old posts, they did this task in very different ways. How
is the following one?
Thank you for your time.
/...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |