| | |
Bitset
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 11
Reputation:
Solved Threads: 0
Im attempting to do bitwise operations on a character string and want to use the bitset template. I am having some trouble, it doesnt seem to like to be constructed using a char* string or std::string... I'm knew to this particular template. Here is what I'm attempting:
std::bitset<3000> Line("abcdefghijklmnop...");
anyone any ideas? Would be much appreciated, I want to get into the mentality of using the C++ STL. :eek:
std::bitset<3000> Line("abcdefghijklmnop...");
anyone any ideas? Would be much appreciated, I want to get into the mentality of using the C++ STL. :eek:
•
•
•
•
Originally Posted by ramcfloyn
any ideas?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <bitset> #include <vector> #include <algorithm> int main() { static const char init[] = "abcdefghijklmnop..."; std::vector<std::bitset<8> > str(init, init + sizeof init); std::copy(str.begin(), str.end(), std::ostream_iterator<std::bitset<8> > (std::cout, "\n")); return 0; } /* my output 01100001 01100010 01100011 01100100 01100101 01100110 01100111 01101000 01101001 01101010 01101011 01101100 01101101 01101110 01101111 01110000 00101110 00101110 00101110 00000000 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jul 2005
Posts: 11
Reputation:
Solved Threads: 0
Thanks Dave. Maybe I should have explained my problem better though. I am getting a character string that represents a line in an image, in order to do a pixel count for that line of the image I need to get the value of individual bits of the characters. Here is what I have written:
[PHP]#include <iostream>
#include <bitset>
#include <cstring>
#include <stdlib>
int main()
{
long pixelCount = 0;
char* pLine;
long startX = 10; //arbitrary starting point on line
long endX = 45; //arbitrary end point on line
/* section of code that returns a char string pLine
this line is 312 bytes long*/
pLine = "HELLO"; //Used for example
for(long x = startX; x < endX; x++)
{
char init = pLine[x]; //retrieving individual char
std::bitset<8> bits(init);
if(bits.any()) //checking if any bits of the char are on
{
for(int i = 0; i < 7; i++)
{
if(bits.test(i))
{
pixelCount++; //test each bit and if set increment count
}
}
}
}
std::cout << pixelCount << std::endl;
system("PAUSE");
return 0;
}[/PHP]
Giving a count of 35
Originally I wanted to use the bitset::count on the entire char string but it wasn't possible to set up a bitset with the character string, so i tried to use it on just individual chars but got a compile error in Borland:
[Linker Error] Unresolved external '_STL::_Bs_G<bool>::_S_bit_count' referenced from C:\DOCUMENTS AND SETTINGS\MY DOCUMENTS\SRC
i was calling it like so:
[PHP] bits.count();[/PHP]
The way I have done it is working fine, just thought if the bitset::count member was function was availaiable it would increase speed?!
[PHP]#include <iostream>
#include <bitset>
#include <cstring>
#include <stdlib>
int main()
{
long pixelCount = 0;
char* pLine;
long startX = 10; //arbitrary starting point on line
long endX = 45; //arbitrary end point on line
/* section of code that returns a char string pLine
this line is 312 bytes long*/
pLine = "HELLO"; //Used for example
for(long x = startX; x < endX; x++)
{
char init = pLine[x]; //retrieving individual char
std::bitset<8> bits(init);
if(bits.any()) //checking if any bits of the char are on
{
for(int i = 0; i < 7; i++)
{
if(bits.test(i))
{
pixelCount++; //test each bit and if set increment count
}
}
}
}
std::cout << pixelCount << std::endl;
system("PAUSE");
return 0;
}[/PHP]
Giving a count of 35
Originally I wanted to use the bitset::count on the entire char string but it wasn't possible to set up a bitset with the character string, so i tried to use it on just individual chars but got a compile error in Borland:
[Linker Error] Unresolved external '_STL::_Bs_G<bool>::_S_bit_count' referenced from C:\DOCUMENTS AND SETTINGS\MY DOCUMENTS\SRC
i was calling it like so:
[PHP] bits.count();[/PHP]
The way I have done it is working fine, just thought if the bitset::count member was function was availaiable it would increase speed?!
![]() |
Similar Threads
- Can someone please help me with Binary Addtion. (C++)
- conversion to binary (C++)
- Exercise using: unsigned char bcd(int n); (C)
- Exercise using: unsigned int datecode(int year, int month, int day); (C++)
Other Threads in the C++ Forum
- Previous Thread: Need Help Please.....
- Next Thread: file query
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






