If the string given is 01111 and text is 0111101111 , what is the number of bits required in bit stuffing?
#include <iostream>
#include <string>
int main() {
// read bits '0'/'1' into a string; terminate on any other char
std::string data_stream ;
char bit ;
while( std::cin >> bit && ( bit == '0' || bit == '1' ) ) data_stream += bit ;
// stuff a zero bit after four consecutive bits of the same value.
std::string stuffed_stream ;
int cnt = 0 ;
char bit_last_seen = 0 ;
int stuff_bits_added = 0 ;
for each bit in the data_stream
{
if( it is the same as the previous bit ) increment cnt
else // it is a different bit
{
bit_last_seen := this bit ;
cnt := 1 ; // restart count at 1
}
stuffed_stream += bit ; // add the bit
if( cnt == 4 ) // there are four consecutive bits of the same value
{
stuffed_stream += '0' ; // stuff with a zero bit
cnt := 0 ; // and reset cnt to zero
++stuff_bits_added ; // increment the count of stuff bits added
}
}
// print out the results
std::cout << " data stream: " << data_stream << '\n'
<< " stuffed stream: " << stuffed_stream << '\n'
<< "stuff bits added: " << stuff_bits_added << '\n' ;
// TODO:
// The de-stuffing code to process the stuffed data to recreate the original
// and verify that the original data is recovered correctly.
}
Some pseudo code from http://www.cplusplus.com/forum/general/98631/#msg533723
Now what seems to be your problem?
Related
I am trying to make a comparison search function that can compare two strings together based on chars. For example:
Input: "ca"
Struct vector contents under last name fields(sorted vector)(index/value): 0/"cars", 1/"roads"
Result: "Name Found at index 0 - cars"
I want users to be able to use any number of chars. The program should then compare the provided search criteria to the content of the vector and return the index of any matches.
So far I have tried to implement this alogorithm without any success, here is my code so far. Also I am pretty new to C++.
// Function for searching through an array for a string value.
int searchArray(std::vector<playerdata> (&people), std::string name) {
int loc = -1;
int counter = 0;
int index = 0;
//when loc is no longer -1, that means the person has been found
for (int i = 0; i < people.size(); i++)
for(int k = 0; k < name.length(); k++) {
std::cout << name[k-1] << std::endl;
std::cout << people[i].lastname[k-1] << std::endl;
std::cout << counter << std::endl;
std::cout << "" << std::endl;
if(name[k-1] == people[i].lastname[k-1]) {
counter++;
}
if(counter == name.length()) {
loc = i;
break;
}
}
//if (people[i].lastname.compare(name) == 0)
//loc = i;
return loc;
}
Here is what I get in my console, I am using cout to debug:
What is player 1 information (F/L/DOB (DD/MM/YYY)), Seperate using a space):
hello sunshine
What is player 2 information (F/L/DOB (DD/MM/YYY)), Seperate using a space):
good bye
Pick from the available options:
1 - Input Data:
2 - Display Original Data:
3 - Sort Data:
4 - Display Sorted Data:
5 - Search By Last Name:
6 - Exit The Program;
3
Array Sorted!!!
Pick from the available options:
1 - Input Data:
2 - Display Original Data:
3 - Sort Data:
4 - Display Sorted Data:
5 - Search By Last Name:
6 - Exit The Program;
4
Player 1: good bye
Player 2: hello sunshine
Pick from the available options:
1 - Input Data:
2 - Display Original Data:
3 - Sort Data:
4 - Display Sorted Data:
5 - Search By Last Name:
6 - Exit The Program;
5
Enter the name to search:
bye
b
b
0
y
y
1
e
e
2
b
s
3
y
u
4
e
n
4
Player Found: good bye
Enter the name to search:
by
y
b
0
y
s
1
Player Found: good bye
Enter the name to search:
b
The player was not found, try again.
Enter the name to search:
sun
u
b
0
n
y
1
u
s
1
Player Found: hello sunshine
Enter the name to search:
sunshine
u
b
0
n
y
1
s
e
1
h
1
i
h
1
n
i
1
e
n
1
u
s
1
n
u
2
s
n
2
h
s
2
i
h
2
n
i
2
e
n
2
The player was not found, try again.
Enter the name to search:
EDIT: As you can see from my console output the code is returning true comparisons when it shouldn't be doing so. An example is the last comparison of is (e == n). The answer should be false but it keeps returning true.
After using code suggested in the comments I still cant get my code to work as expected and get the following errors:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
E:\Coding\Cplus_work\assignmentseven.cpp||In function 'int main()':|
E:\Coding\Cplus_work\assignmentseven.cpp|68|warning: NULL used in arithmetic [-Wpointer-arith]|
E:\Coding\Cplus_work\assignmentseven.cpp|158|warning: NULL used in arithmetic [-Wpointer-arith]|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\predefined_ops.h||In instantiation of 'bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<playerdata*, std::vector<playerdata> >; _Predicate = searchArray(std::vector<playerdata>&, std::__cxx11::string&)::<lambda(std::__cxx11::string&)>]':|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\stl_algo.h|120|required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<playerdata*, std::vector<playerdata> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<searchArray(std::vector<playerdata>&, std::__cxx11::string&)::<lambda(std::__cxx11::string&)> >]'|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\stl_algo.h|161|required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<playerdata*, std::vector<playerdata> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<searchArray(std::vector<playerdata>&, std::__cxx11::string&)::<lambda(std::__cxx11::string&)> >]'|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\stl_algo.h|3930|required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<playerdata*, std::vector<playerdata> >; _Predicate = searchArray(std::vector<playerdata>&, std::__cxx11::string&)::<lambda(std::__cxx11::string&)>]'|
E:\Coding\Cplus_work\assignmentseven.cpp|35|required from here|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\predefined_ops.h|283|error: no match for call to '(searchArray(std::vector<playerdata>&, std::__cxx11::string&)::<lambda(std::__cxx11::string&)>) (playerdata&)'|
E:\Coding\Cplus_work\assignmentseven.cpp|33|note: candidate: 'searchArray(std::vector<playerdata>&, std::__cxx11::string&)::<lambda(std::__cxx11::string&)>'|
E:\Coding\Cplus_work\assignmentseven.cpp|33|note: no known conversion for argument 1 from 'playerdata' to 'std::__cxx11::string&' {aka 'std::__cxx11::basic_string<char>&'}|
E:\Coding\Cplus_work\assignmentseven.cpp||In function 'bool sortArray(const playerdata&, const playerdata&)':|
E:\Coding\Cplus_work\assignmentseven.cpp|28|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 1 error(s), 8 warning(s) (0 minute(s), 0 second(s)) ===|
Exact code I used:
// Function for searching through an array for a string value.
int searchArray(std::vector<playerdata> (&people), std::string (&name))
{
auto it = std::find_if(people.begin(), people.end(), [&name](std::string& person){
return person.find(name) != std::string::npos;
});
if(it != people.end()) {
return std::distance(people.begin(), it);
} else {
return -1;
}
}
Probably the main problem is that you are trying to access name[k-1] and lastname[k-1], when k=0, which results in UB.
Before continuing with your work, start learning the STL algorithms. With that in mind, your task becomes trivial using only find_if and std::string::find:
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
struct playerdata
{
std::string lastname;
playerdata(std::string lastname) :
lastname(std::move(lastname))
{
}
};
int searchArray(std::vector<playerdata>& people, const std::string& name)
{
auto it = std::find_if(people.cbegin(), people.cend(), [&name](const playerdata& player){
return player.lastname.find(name) != std::string::npos;
});
if(it != people.end())
return std::distance(people.cbegin(), it);
else
return -1;
}
}
LIVE DEMO
First of all your approach to solution is correct but needs some
improvement.
I assumed that you don`t want to solve it using regular expression or a template c++ function
which would be better way
You may have a misunderstanding about break statement which affects
only the loop it is written in break
I've explained missing points and required improvements in comment
lines
Since you didn`t specify all details I had to assume them.
`
SearchArray(std::vector<playerdata>& people, const std::string& keyWord)
{
int loc = -1;
// Use a better variable name to explain its purpose instead of `i`
// so you and other people like us can understand its meaning much faster
for (int vectorIndex = 0; vectorIndex < people.size(); vectorIndex++)
{
// if length of the name is bigger than a lastname it can`t be a match so just continue
if (keyWord.length() > people[vectorIndex].lastname.length())
{
continue;
}
int counter = 0;
int charIndexKeyWord = 0;
// Use a better variable name to explain its purpose instead of `k`
for (int charIndexIarget = 0; charIndexIarget < people[vectorIndex].lastname.length(); charIndexIarget++)
{
// Your for loop starts from 0 but you're trying to use [k-1] in your code
// you shouldn't do that, because in the first iteration of the loop it will indicate -1 [k-1]
// but it starts with 0,
//std::cout << keyWord[charIndexKeyWord] << std::endl;
//std::cout << people[vectorIndex].lastname[charIndexIarget] << std::endl;
//std::cout << counter << std::endl;
//std::cout << "" << std::endl;
// This is part of your code which needs improvment
if (keyWord[charIndexKeyWord] == people[vectorIndex].lastname[charIndexIarget])
{
counter++;
charIndexKeyWord++;
}
else
{
// If you keep your code as it is, it will may also consider matches like susnhine `sun` because
// so you need to make some improvements to get rid of it.
// if you have already founded a match but next word is not a match then you need to start looking from the beginning
if (counter != 0)
{
counter = 0;
charIndexKeyWord = 0;
}
}
if (counter == keyWord.length())
{
loc = vectorIndex;
}
}
if (loc != -1)
{
break;
}
}
return loc;
}
I have very recently started using vectors in C++.The purpose of the program I am trying to write is to determine the first missing positive number. Given an input array A : [ -9, 12, -1, 0, 1 ] the expected output is 2. I coded it -
int Solution::firstMissingPositive(vector<int> &A)
{
std::sort(A.begin(),A.end()); //sorting vector
bool Negative=false;
int flag;
int i=0;
for(i=0;i<A.size();++i)
{
if(A[i]<=0)
{
Negative=true;
std::cout<<"\nwe found a -ve number or 0";
}
else if( (A[i]>0) && (A[i+1]!=A[i]+1) )
{
Negative=false;
std::cout<<"\ncomparing # ="<<i<<" which = "<<A[i];
std::cout<<"\ncomparing # ="<<i+1<<" which = "<<A[i+1];
flag=A[i]+1; //The faulty statement
std::cout<<"\n\n missing number(A[i]+1) # ="<<A[i]+1;
std::cout<<"\n\n missing number(flag) # ="<<flag;
break;
}
}
//do something more
}
With this output -
-9 -1 0 1 12
we found a -ve number or 0
we found a -ve number or 0
we found a -ve number or 0
comparing # =3 which = 1
comparing # =4 which = 12
missing number(A[i]+1) # =2
missing number(flag) # =20
I found this interesting because, to me, it looks like I cannot use an integer to store the value of a vector.
Trying to debug it I found changing the flag assignment to flag = A[i]+2 makes the resultant print 30.
I've read other questions on SO where it suggests using vector.at(i) instead of the [] operator as a better practice. Changing this does not reflect any change for my code.
Changing flag to vector<int> gives me a dirty error which I'm not sure about.
Isn't A[i]+1 syntactically equivalent to an integer value? And if it is, why can I not store it?
Reducing your code to an MCVE quickly demonstrates that the problem is with your output formatting, putting the "\n" at the beginning of the statement is an uncommon practice for precisely this reason. You're not seeing 20, what you're seeing is the "2" from flag followed by a zero printed elsewhere.
#include <algorithm>
#include <iostream>
#include <vector>
void solve(std::vector<int>& A)
{
std::sort(A.begin(),A.end()); //sorting vector
bool Negative=false;
int flag;
int i=0;
for(i=0;i<A.size();++i)
{
if(A[i]<=0)
{
Negative=true;
std::cout<<"we found a -ve number or 0\n";
}
else if( (A[i]>0) && (A[i+1]!=A[i]+1) )
{
Negative=false;
std::cout<<"comparing # ="<<i<<" which = "<<A[i]<<'\n';
std::cout<<"comparing # ="<<i+1<<" which = "<<A[i+1]<<'\n';
flag=A[i]+1; //The faulty statement
std::cout<<"missing number(A[i]+1) # ="<<A[i]+1<<'\n';
std::cout<<"missing number(flag) # ="<<flag<<'\n';
break;
}
}
//do something more
}
int main() {
std::vector<int> A { -9, 12, -1, 0, 1 };
solve(A);
std::cout << "and we're done\n";
}
Output (see http://ideone.com/zb9fNX)
we found a -ve number or 0
we found a -ve number or 0
we found a -ve number or 0
comparing # =3 which = 1
comparing # =4 which = 12
missing number(A[i]+1) # =2
missing number(flag) # =2
and we're done
I should also point out that your test for "A[i+1]" will lead to an out of bounds array access if it ever tries to read the last element in the array. You should change
for(i=0;i<A.size();++i)
to
for(i=0;i<A.size() - 1;++i)
or more conventially,
for(i=1;i<A.size();++i)
and use "A[i-1]" instead of "A[i+1]" to fetch the previous value.
For example:
#include <algorithm>
#include <iostream>
#include <vector>
void solve(std::vector<int>& A)
{
std::sort(A.begin(),A.end()); //sorting vector
int expected = 0;
int missing = 0;
for(int i = 1; i < A.size(); ++i)
{
if (A[i] <= 0) {
std::cout << "we found " << A[i] << "\n";
continue;
}
if (A[i-1] <= 0) {
expected = 1;
} else {
expected = A[i-1] + 1;
}
if (A[i] == expected)
continue;
std::cout << "A[" << i-1 << "] = " << A[i-1] << '\n';
std::cout << "expecting " << expected << '\n';
std::cout << "A[" << i << "] = " << A[i] << '\n';
missing = expected;
std::cout << "missing number: " << expected << '\n';
break;
}
//do something more
}
int main() {
std::vector<int> A { -9, 12, -1, 0, 1 };
solve(A);
std::cout << "and we're done\n";
}
http://ideone.com/AIw4oU
I have tried to fill a smaller vector of a vector of pairs with some contents from a bigger vector of a vector of pairs without success. Below is the relevant code with couts and their output. Hopefully this is detailed enough.
/*******************Problems Occur*****************/
int iFirst=problemsStartAt;//first index to copy
int iLast=problemsEndAt-1;//last index -1, 11th stays
int iLen=iLast-iFirst;//10-8=2
//if(problemsStartAt!=0)//I.a
if(problemsStartAt!=0&&problemsEndAt!=0)//I.b
{
v_problem_temp=allPathCoordinates[problemsStartAt];
cout<<"266:"<<v_problem_temp.size()<<endl;
cout<<"267:"<<allPathCoordinates.at(1).size()<<endl;
for(vector<pair<int,int>>::iterator it2=v_problem_temp.begin();
it2!=v_problem_temp.end();
++it2)
{
apair=*it2;
point[apair.first][apair.second]=Yellow;
cout<<apair.first<<","<<apair.second<<endl;
}
problemsPathCoordinate.resize(iLen);
cout<<"iLen*sizeof(problemsPathCoordinate):" <<iLen*sizeof(problemsPathCoordinate)<<endl;
memcpy(&problemsPathCoordinate[0],&allPathCoordinates[iFirst],iLen*sizeof(problemsPathCoordinate));
cout<<"279:problemsPathCoordinate.size():"<<problemsPathCoordinate.size()<<endl;
problemsPathCoordinate.resize(iLen);
memcpy(&problemsPathCoordinate[0],&allPathCoordinates[iFirst],iLen*sizeof(problemsPathCoordinate));
cout<<"283:problemsPathCoordinate.size():"<<problemsPathCoordinate[0].size()<<endl;
cout<<"284:problemsPathCoordinate.size():"<<problemsPathCoordinate[1].size()<<endl;
cout<<"286:allPathCoordinates.size():"<<allPathCoordinates.size()<<endl;
cout<<"287:allPathCoordinates.size():"<<allPathCoordinates.size()<<endl;
//from http://stackoverflow.com/questions/35265577/c-reverse-a-smaller-range-in-a-vector
}
Output:
759: path NOT full-filled, number: 8
755: Problems START here at:8
759: path NOT full-filled, number: 9
700: Problems END here at: 11
266:0
267:0
iLen*sizeof(problemsPathCoordinate):72
279:problemsPathCoordinate.size():3
283:problemsPathCoordinate.size():0
284:problemsPathCoordinate.size():0
286:allPathCoordinates.size():79512
287:allPathCoordinates.size():79512
time:39 seconds
Why are the three problemsPathCoordinate elements empty. How to fix it?
Bo
for (vector< vector > >::iterator it = allPathCoordinates.begin(); it != allPathCoordinates.end(); ++it)
{
allPathCoordinates.erase(allPathCoordinates.begin()+5,allPathCoordinates.end()-2);
v_temp = *it;
//cout<<"v_temp.size():"<
for (vector<pair<int,int> >::iterator it2 = v_temp.begin(); it2 != v_temp.end(); ++it2) {
//v_temp.erase(v_temp.begin()+2);
apair = *it2;
//cout << "(" << apair.first << "," << apair.second << ") ; ";
openPoints[apair.first][apair.second]=0;
closedPoints[apair.first][apair.second]=1;
allObstacles[apair.first][apair.second]=Wall;
point[apair.first][apair.second]=Yellow;
}
/
I just saw that this could technically work, the only mistake I couldn´t resolve was the last ASCII character that gets printed everytime I test it out, I also tested this out without using the name variable, I mean just making a substraction of 32 to any lower case letter in ASCII should give me their upper case one and it does, but I´m curious on why I´m getting an additional char, wich from what I see in screen is apparently Û.
#include <stdio.h>
main()
{
char name[22];
int i;
fputs("Type your name ",stdout);
fgets(name,22,stdin);
for (i = 0; name[i] != '\0'; i = i + 1)
printf("%c",(name[i])-32); /*This will convert lower case to upper */
/* using as reference the ASCII table*/
fflush(stdin);
getchar();
}
Perhaps there is a line break character at the end of the string.
You can check the chararacter code, so that you only convert characters that actually are lower case letters:
for (i = 0; name[i] != '\0'; i = i + 1) {
char c = name[i];
if (c => 97 && c <= 122) {
c -= 32;
}
printf("%c", c);
}
void read_chararray(char in_array[], int* Length)
{
int Indx = 0, Indx2 = 0, Indx3 = 0; // int declarations for indexs of some loops
char cinput = { 0 }, word[255] = { 0 }, word2[255] = { 0 }; // declaration of cinput and first char array before punctiation removed
for (Indx = 0; (cinput = getchar()) != '\n'; Indx++) { // Loop for getting characters from user stop at <enter>
word[Indx] = cinput; // Placing char into array while changing to lowercase
}
Indx2 = Indx; // Set Indx2 to Indx for loop operation
for (Indx = 0; Indx < Indx2; Indx++) { // Loop to check and replace upper characters with lower
cinput = word[Indx];
if (cinput >= 65 && cinput <= 90) { // If cinput is within the ASCII range 65 and 90, this indicates upper characters
cinput += 32; // Add 32 to cinput to shift to the lower character range within the ASCII table
in_array[Indx] = cinput; // Input new value into array pointer
}
else if (cinput >= 97 && cinput <= 122) // scans if character are lower ASCII, places them in array irraticating punctuation and whitespce
in_array[Indx] = cinput; // Input remaining lower case into array pointer
}
*Length = Indx; // final size of array set to Length variable for future use
}
#include<stdio.h>
void upper(char);
void main()
{
char ch;
printf("\nEnter the character in lower case");
scanf("%c", &ch);
upper(ch);
}
void upper( char c)
{
printf("\nUpper Case: %c", c-32);
}
Given an 32 bit int which is known to have at least 2 bits set, is there a way to efficiently clear all except the 2 most significant set bits? i.e. I want to ensure the output has exactly 2 bits set.
What if the input is guaranteed to have only 2 or 3 bits set.?
Examples:
0x2040 -> 0x2040
0x0300 -> 0x0300
0x0109 -> 0x0108
0x5040 -> 0x5000
Benchmarking Results:
Code:
QueryPerformanceFrequency(&freq);
/***********/
value = (base =2)|1;
QueryPerformanceCounter(&start);
for (l=0;l<A_LOT; l++)
{
//!!value calculation goes here
junk+=value; //use result to prevent optimizer removing it.
//advance to the next 2|3 bit word
if (value&0x80000000)
{ if (base&0x80000000)
{ base=6;
}
base*=2;
value=base|1;
}
else
{ value<<=1;
}
}
QueryPerformanceCounter(&end);
time = (end.QuadPart - start.QuadPart);
time /= freq.QuadPart;
printf("--------- name\n");
printf("%ld loops took %f sec (%f additional)\n",A_LOT, time, time-baseline);
printf("words /sec = %f Million\n",A_LOT/(time-baseline)/1.0e6);
Results on using VS2005 default release settings on Core2Duo E7500#2.93 GHz:
--------- BASELINE
1000000 loops took 0.001630 sec
--------- sirgedas
1000000 loops took 0.002479 sec (0.000849 additional)
words /sec = 1178.074206 Million
--------- ashelly
1000000 loops took 0.004640 sec (0.003010 additional)
words /sec = 332.230369 Million
--------- mvds
1000000 loops took 0.005250 sec (0.003620 additional)
words /sec = 276.242030 Million
--------- spender
1000000 loops took 0.009594 sec (0.007964 additional)
words /sec = 125.566361 Million
--------- schnaader
1000000 loops took 0.025680 sec (0.024050 additional)
words /sec = 41.580158 Million
If the input is guaranteed to have exactly 2 or 3 bits then the answer can be computed very quickly. We exploit the fact that the expression x&(x-1) is equal to x with the LSB cleared. Applying that expression twice to the input will produce 0, if 2 or fewer bits are set. If exactly 2 bits are set, we return the original input. Otherwise, we return the original input with the LSB cleared.
Here is the code in C++:
// assumes a has exactly 2 or 3 bits set
int topTwoBitsOf( int a )
{
int b = a&(a-1); // b = a with LSB cleared
return b&(b-1) ? b : a; // check if clearing the LSB of b produces 0
}
This can be written as a confusing single expression, if you like:
int topTwoBitsOf( int a )
{
return a&(a-1)&((a&(a-1))-1) ? a&(a-1) : a;
}
I'd create a mask in a loop. At the beginning, the mask is 0. Then go from the MSB to the LSB and set each corresponding bit in the mask to 1 until you found 2 set bits. Finally AND the value with this mask.
#include <stdio.h>
#include <stdlib.h>
int clear_bits(int value) {
unsigned int mask = 0;
unsigned int act_bit = 0x80000000;
unsigned int bit_set_count = 0;
do {
if ((value & act_bit) == act_bit) bit_set_count++;
mask = mask | act_bit;
act_bit >>= 1;
} while ((act_bit != 0) && (bit_set_count < 2));
return (value & mask);
}
int main() {
printf("0x2040 => %X\n", clear_bits(0x2040));
printf("0x0300 => %X\n", clear_bits(0x0300));
printf("0x0109 => %X\n", clear_bits(0x0109));
printf("0x5040 => %X\n", clear_bits(0x5040));
return 0;
}
This is quite complicated, but should be more efficient as using a for loop over the 32 bits every time (and clear all bits except the 2 most significant set ones). Anyway, be sure to benchmark different ways before using one.
Of course, if memory is not a problem, use a lookup table approach like some recommended - this will be much faster.
how much memory is available at what latency? I would propose a lookup table ;-)
but seriously: if you would perform this on 100s of numbers, an 8 bit lookup table giving 2 msb and another 8 bit lookup table giving 1 msb may be all you need. Depending on the processor this might beat really counting bits.
For speed, I would create a lookup table mapping an input byte to
M(I)=0 if 1 or 0 bits set
M(I)=B' otherwise, where B' is the value of B with the 2 msb bits set.
Your 32 bit int are 4 input bytes I1 I2 I3 I4.
Lookup M(I1), if nonzero, you're done.
Compare M(I1)==0, if zero, repeat previous step for I2.
Else, lookup I2 in a second lookup table with 1 MSB bits, if nonzero, you're done.
Else, repeat previous step for I3.
etc etc. Don't actually loop anything over I1-4 but unroll it fully.
Summing up: 2 lookup tables with 256 entries, 247/256 of cases are resolved with one lookup, approx 8/256 with two lookups, etc.
edit: the tables, for clarity (input, bits table 2 MSB, bits table 1 MSB)
I table2 table1
0 00000000 00000000
1 00000000 00000001
2 00000000 00000010
3 00000011 00000010
4 00000000 00000100
5 00000101 00000100
6 00000110 00000100
7 00000110 00000100
8 00000000 00001000
9 00001001 00001000
10 00001010 00001000
11 00001010 00001000
12 00001100 00001000
13 00001100 00001000
14 00001100 00001000
15 00001100 00001000
16 00000000 00010000
17 00010001 00010000
18 00010010 00010000
19 00010010 00010000
20 00010100 00010000
..
250 11000000 10000000
251 11000000 10000000
252 11000000 10000000
253 11000000 10000000
254 11000000 10000000
255 11000000 10000000
Here's another attempt (no loops, no lookup, no conditionals). This time it works:
var orig=0x109;
var x=orig;
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
x = orig & ~(x & ~(x >> 1));
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
var solution=orig & ~(x >> 1);
Console.WriteLine(solution.ToString("X")); //0x108
Could probably be shortened by someone cleverer than me.
Following up on my previous answer, here's the complete implementation. I think it is as fast as it can get. (sorry for unrolling the whole thing ;-)
#include <stdio.h>
unsigned char bittable1[256];
unsigned char bittable2[256];
unsigned int lookup(unsigned int);
void gentable(void);
int main(int argc,char**argv)
{
unsigned int challenge = 0x42341223, result;
gentable();
if ( argc > 1 ) challenge = atoi(argv[1]);
result = lookup(challenge);
printf("%08x --> %08x\n",challenge,result);
}
unsigned int lookup(unsigned int i)
{
unsigned int ret;
ret = bittable2[i>>24]<<24; if ( ret ) return ret;
ret = bittable1[i>>24]<<24;
if ( !ret )
{
ret = bittable2[i>>16]<<16; if ( ret ) return ret;
ret = bittable1[i>>16]<<16;
if ( !ret )
{
ret = bittable2[i>>8]<<8; if ( ret ) return ret;
ret = bittable1[i>>8]<<8;
if ( !ret )
{
return bittable2[i] | bittable1[i];
} else {
return (ret | bittable1[i&0xff]);
}
} else {
if ( bittable1[(i>>8)&0xff] )
{
return (ret | (bittable1[(i>>8)&0xff]<<8));
} else {
return (ret | bittable1[i&0xff]);
}
}
} else {
if ( bittable1[(i>>16)&0xff] )
{
return (ret | (bittable1[(i>>16)&0xff]<<16));
} else if ( bittable1[(i>>8)&0xff] ) {
return (ret | (bittable1[(i>>8)&0xff]<<8));
} else {
return (ret | (bittable1[i&0xff]));
}
}
}
void gentable()
{
int i;
for ( i=0; i<256; i++ )
{
int bitset = 0;
int j;
for ( j=128; j; j>>=1 )
{
if ( i&j )
{
bitset++;
if ( bitset == 1 ) bittable1[i] = i&(~(j-1));
else if ( bitset == 2 ) bittable2[i] = i&(~(j-1));
}
}
//printf("%3d %02x %02x\n",i,bittable1[i],bittable2[i]);
}
}
Using a variation of this, I came up with the following:
var orig=56;
var x=orig;
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
Console.WriteLine(orig&~(x>>2));
In c# but should translate easily.
EDIT
I'm not so sure I've answered your question. This takes the highest bit and preserves it and the bit next to it, eg. 101 => 100
Here's some python that should work:
def bit_play(num):
bits_set = 0
upper_mask = 0
bit_index = 31
while bit_index >= 0:
upper_mask |= (1 << bit_index)
if num & (1 << bit_index) != 0:
bits_set += 1
if bits_set == 2:
num &= upper_mask
break
bit_index -= 1
return num
It makes one pass over the number. It builds a mask of the bits that it crosses so it can mask off the bottom bits as soon as it hits the second-most significant one. As soon as it finds the second bit, it proceeds to clear the lower bits. You should be able to create a mask of the upper bits and &= it in instead of the second while loop. Maybe I'll hack that in and edit the post.
I'd also use a table based approach, but I believe one table alone should be sufficient. Take the 4 bit case as an example. If you're input is guaranteed to have 2 or 3 bits, then your output can only be one of 6 values
0011
0101
0110
1001
1010
1100
Put these possible values in an array sorted by size. Starting with the largest, find the first value which is equal to or less than your target value. This is your answer. For the 8 bit version you'll have more possible return values, but still easily less than the maximum possible permutations of 8*7.
public static final int [] MASKS = {
0x03, //0011
0x05, //0101
0x06, //0110
0x09, //1001
0x0A, //1010
0x0C, //1100
};
for (int i = 0; i < 16; ++i) {
if (countBits(i) < 2) {
continue;
}
for (int j = MASKS.length - 1; j >= 0; --j) {
if (MASKS[j] <= i) {
System.out.println(Integer.toBinaryString(i) + " " + Integer.toBinaryString(MASKS[j]));
break;
}
}
}
Here's my implementation in C#
uint OnlyMostSignificant(uint value, int count) {
uint newValue = 0;
int c = 0;
for(uint high = 0x80000000; high != 0 && c < count; high >>= 1) {
if ((value & high) != 0) {
newValue = newValue | high;
c++;
}
}
return newValue;
}
Using count, you could make it the most significant (count) bits.
My solution:
Use "The best method for counting bits in a 32-bit integer", then clear the lower bit if the answer is 3. Only works when input is limited to 2 or 3 bits set.
unsigned int c; // c is the total bits set in v
unsigned int v = value;
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
crc+=value&value-(c-2);