473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Bitwise manipulation

how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.

Jul 14 '07 #1
8 2602

"Nehil" <ne***********@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.
Take the binary complement of the number (~). Take two copies, and shift one
a bit (<<). Then AND (&, just one). If the answer is non-zero, you have
consecutive zero bits.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 14 '07 #2
On Jul 15, 2:02 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Nehil" <nehilparas...@gmail.comwrote in message

news:11**********************@22g2000hsm.googlegro ups.com...how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.

Take the binary complement of the number (~). Take two copies, and shift one
a bit (<<). Then AND (&, just one). If the answer is non-zero, you have
consecutive zero bits.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
Thanks for your answer. but the solution you suggested only tells true
or false, whether consecutive zero bits are there or not.

i need something different :
i want to know how many consecutive zero bits are there?
for exapmle :
let say arr[0] = 10; so bit representation will be (32-bit)

00000000 00000000 00000000 00001010

so answer should be =29 from MSB and 1 from LSB.
please clarify.
Thanks.

Jul 14 '07 #3
Nehil wrote:
how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.
Are you the same "Nehil" who is undertaking to write
a garbage collector for C? If so, have you ever heard the
advice "Walk before you run?"

Yes, we all know that "A man's reach should exceed his
grasp," but perhaps by not quite so wide a span ...

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 14 '07 #4
On Jul 15, 2:30 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Nehil wrote:
how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.

Are you the same "Nehil" who is undertaking to write
a garbage collector for C? If so, have you ever heard the
advice "Walk before you run?"

Yes, we all know that "A man's reach should exceed his
grasp," but perhaps by not quite so wide a span ...

--
Eric Sosman
esos...@ieee-dot-org.invalid
================================================== =================
Yes, i'm the same Nehil. :) but i could not understand your wordings.
i
came to this doubt in that project only. i designed a bitmap for each
page i allocate, and now want to know how many pages have been
allocated by knowing how many bits are there zero??

is this approach good?

Jul 14 '07 #5
Nehil wrote:
>
On Jul 15, 2:02 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Nehil" <nehilparas...@gmail.comwrote in message

news:11**********************@22g2000hsm.googlegro ups.com...how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.
Take the binary complement of the number (~). Take two copies, and shift one
a bit (<<). Then AND (&, just one). If the answer is non-zero, you have
consecutive zero bits.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

Thanks for your answer. but the solution you suggested only tells true
or false, whether consecutive zero bits are there or not.

i need something different :
i want to know how many consecutive zero bits are there?
for exapmle :
let say arr[0] = 10; so bit representation will be (32-bit)

00000000 00000000 00000000 00001010

so answer should be =29 from MSB and 1 from LSB.
please clarify.
/* BEGIN new.c */

#include <stdio.h>

#define N 10U
#define str(s) # s
#define xstr(s) str(s)

void consecutive_zero_bits(unsigned n, unsigned *most, unsigned *least);

int main(void)
{
unsigned most, least;

consecutive_zero_bits(N, &most, &least);
puts(xstr(N));
printf("%u from MSB and %u from LSB.\n", most, least);
return 0;
}

void consecutive_zero_bits(unsigned n, unsigned *most, unsigned *least)
{
unsigned mask, count;

count = 0;
for (mask = 1; mask != 0; mask *= 2) {
if( (mask & n) == 0) {
++count;
} else {
*least = count;
break;
}
}
count = 0;
for (mask = (0u - 1) / 2 + 1; mask != 0; mask /= 2) {
if( (mask & n) == 0) {
++count;
} else {
*most = count;
break;
}
}
}

/* END new.c */

--
pete
Jul 14 '07 #6

"Nehil" <ne***********@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
On Jul 15, 2:30 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>Nehil wrote:
how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.

Are you the same "Nehil" who is undertaking to write
a garbage collector for C? If so, have you ever heard the
advice "Walk before you run?"

Yes, we all know that "A man's reach should exceed his
grasp," but perhaps by not quite so wide a span ...

--
Eric Sosman
esos...@ieee-dot-org.invalid

================================================== =================
Yes, i'm the same Nehil. :) but i could not understand your wordings.
i
came to this doubt in that project only. i designed a bitmap for each
page i allocate, and now want to know how many pages have been
allocated by knowing how many bits are there zero??

is this approach good?
A garbage collector, let alone an efficient garbage collector, is a very
difficult routine to write. If you can't even do basic bit manipulation - no
shame on you, we were all beginners at some time - then there is no way you
are going to achieve a garbage collector, except by some miracle.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jul 15 '07 #7
On Jul 15, 1:33 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Nehil" <nehilparas...@gmail.comwrote in message

news:11**********************@o61g2000hsh.googlegr oups.com...
On Jul 15, 2:30 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Nehil wrote:
how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.
Are you the same "Nehil" who is undertaking to write
a garbage collector for C? If so, have you ever heard the
advice "Walk before you run?"
Yes, we all know that "A man's reach should exceed his
grasp," but perhaps by not quite so wide a span ...
--
Eric Sosman
esos...@ieee-dot-org.invalid
================================================== =================
Yes, i'm the same Nehil. :) but i could not understand your wordings.
i
came to this doubt in that project only. i designed a bitmap for each
page i allocate, and now want to know how many pages have been
allocated by knowing how many bits are there zero??
is this approach good?

A garbage collector, let alone an efficient garbage collector, is a very
difficult routine to write. If you can't even do basic bit manipulation - no
shame on you, we were all beginners at some time - then there is no way you
are going to achieve a garbage collector, except by some miracle.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
Thanks all for your answers and suggestions.
However i've developed it myself and it is working efficiently. it was
bit manipulation that makes me nervous while thinking logic for it.
but i'll practice for it.

the allocator and de-allocator both r working fine.
Actually, the problem i asked to find some effecient solution for it.
by linear searching, i'd already done it.

thnkas all.

Jul 15 '07 #8
Nehil wrote:
how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
What does "either in from LSB or MSB" mean?
if they exist in between, then what to do?
Between what?
plz clarify.
I suggest that _you_ clarify.

--
Thad
Jul 17 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
5
by: James Dean | last post by:
I am recoding a project in C#.....i just wanted to know if these are equivalent and give the same result.... old C++ code for ( long loop = 0; loop < ( longWidth_bytes - 1); loop++) { *lpbLine...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
4
by: AMDRIT | last post by:
Gang, I always get confused when it comes to 1's and 0's. I would like to perform a bitwise operation on a value based on checked boxes. Am I doing this right? assuming...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
11
by: Jordan | last post by:
I am trying to rewrite some C source code for a poker hand evaluator in Python. Putting aside all of the comments such as just using the C code, or using SWIG, etc. I have been having problems...
0
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...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.