473,386 Members | 1,621 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,386 software developers and data experts.

Something about bits

I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.

Values that would meet the first criteria is fx.
3
7
15
and the output i need will be:
3 = 2
7 = 3
15 = 4

it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.

Any suggestions?

Thanks in advance.
Regards

Dec 9 '06 #1
9 1294
<za******@gmail.comwrote:
>I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.

Values that would meet the first criteria is fx.
3
7
15
>3 = 2
7 = 3
15 = 4

it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.
Add one, then check to see if it's a power of two (see FAQ 26.12).

This doesn't get you the second part of your answer. For that
you might want to use a loop.

Steve
Dec 10 '06 #2
* za******@gmail.com:
I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.

Values that would meet the first criteria is fx.
3
7
15
and the output i need will be:
3 = 2
7 = 3
15 = 4

it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.

Any suggestions?
First, this is obviously homework, and it would be nice to mention that.

clc++-ers generally don't do other's homework, but we can help with
concrete C++ problems.

But you don't post any concrete C++ problem.

Now if you'd posted this in [comp.programming] it would have been
on-topic there (but also there with the caveat about homework), and
perhaps you'd receive answers such as "check that x+1 has exactly one
1-bit", which reduces the problem to counting 1-bits.

For counting 1-bits in almost any programming language, you can use
repeated division by 2 and checking the remainder each time. In C++ you
can also use the right-shift operator >>. And many other ways; it may
be a good idea to post again when you have some code to present.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 10 '06 #3

Alf P. Steinbach skrev:
* za******@gmail.com:
I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.

Values that would meet the first criteria is fx.
3
7
15
and the output i need will be:
3 = 2
7 = 3
15 = 4

it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.

Any suggestions?

First, this is obviously homework, and it would be nice to mention that.

clc++-ers generally don't do other's homework, but we can help with
concrete C++ problems.
You asume much thinking that this is homework.
>
But you don't post any concrete C++ problem.
Then let me show how i first thought of doing it.

#include <iostream>
#include <bitset>
unsigned int test(unsigned int i) {
std::bitset<32B = i;
int n;
int j;
for (n = 0; n < 32 && B[n] == 1; n++) {}
for (j = n; j < 32 && B[j] == 0; j++) {}
if (j == 32)
return(n);
else
return(0);
}

int main() {
Prime prime;
for(int i = 3; ; i++) {
std::cout << i << " : " << prime.mersenne(i);
std::cin.get();
}
}

It work, but i think we can all agree that it is messy. The reason for
that? I dont have a clue how to do it endless i use log2, which i wont.
And why not use log2? just me being silly i think, but never the less
it can be done without it, i have allready princip, all i need it a
proper way to write the code.
>
Now if you'd posted this in [comp.programming] it would have been
on-topic there (but also there with the caveat about homework), and
perhaps you'd receive answers such as "check that x+1 has exactly one
1-bit", which reduces the problem to counting 1-bits.

For counting 1-bits in almost any programming language, you can use
repeated division by 2 and checking the remainder each time. In C++ you
can also use the right-shift operator >>. And many other ways; it may
be a good idea to post again when you have some code to present.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 10 '06 #4
the line "Prime prime" isnt suposed to be there.

Dec 10 '06 #5

za******@gmail.com wrote:
I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.

Values that would meet the first criteria is fx.
3
7
15
and the output i need will be:
3 = 2
7 = 3
15 = 4
std::bitset has a member function count() which returns the size_t
number of set bits in you binary.
>
it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.

Any suggestions?

Thanks in advance.
Regards
Dec 10 '06 #6
<za******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.

Values that would meet the first criteria is fx.
3
7
15
and the output i need will be:
3 = 2
7 = 3
15 = 4

it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.

Any suggestions?

Thanks in advance.
Regards
Sicne it's homework I won't give code, but will state how I would do it.

I would copy the value into another var so I can modify it.
I would do a for loop for the number of bits in the interger.
If my variable is 0, then all is finished. Grab the current iterator
count and exit for loop
If my variable & 1 is 1, then so far all bits set. shift right my
variable and continue loop
If my variable & 1 is not 1, then there is a 0 somewhere in the middle.
Check failed
end for loop
Dec 11 '06 #7
Hi Steve,

I wonder where the FAQ is.
Thanks~
Steve Pope wrote:
<za******@gmail.comwrote:
I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.

Values that would meet the first criteria is fx.
3
7
15
3 = 2
7 = 3
15 = 4

it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.

Add one, then check to see if it's a power of two (see FAQ 26.12).

This doesn't get you the second part of your answer. For that
you might want to use a loop.

Steve
Dec 11 '06 #8
campos <hu******@gmail.comtopposts,
>I wonder where the FAQ is.
Thanks~
www.parashift.com
Look for 26.12

Steve
Dec 11 '06 #9
za******@gmail.com wrote:
I have a random inteeger that i need to check if meet a certain
criteria.
"Criteria" is a plural -- if there is only one then it is a criterion.
The criteria is that when converted to binary it consist only
of 1's, no 0's.
Hint: what is the result of x & (x+1) if the number does meet that
criterion?
If that criteria is met i allso need to know how many 1's it consist
of.
Right-shift it by one until it is zero , and count how many
shifts were used.

Dec 12 '06 #10

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

Similar topics

19
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
7
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure,...
6
by: barcaroller | last post by:
I couldn't find a message-digest newsgroup, so I posted here. I have a C function that converts a string of arbitrary length to a 32-bit hash value. I realize this is overkill but I used...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
11
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
77
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte...
9
by: Boltar | last post by:
On 32 bit linux with gcc 4.2 I get unexpected results with this code: main() { int bits = 32; printf("%d\n",(int)1 << (int)32); printf("%d\n",(int)1 << bits); }
11
by: JoeC | last post by:
I am working on a graphics program but my question has nothing to do with graphics but trying to get an algorithm to work. I set graphics from a 16x16 grid to bits of a graphic with: bitData =...
10
by: Guillermo_Lopez | last post by:
Hello All, I am using VBA in access to perform some calculations. There is a particular sumation that is wrong (barely). this code is withing a loop. TDist = TDist + TempDist Both TDist...
11
by: spasmous | last post by:
Just wondering.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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:
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...
0
marktang
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,...
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...

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.