943,772 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5475
  • C++ RSS
Sep 8th, 2005
0

Bitset

Expand Post »
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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ramcfloyn is offline Offline
10 posts
since Jul 2005
Sep 8th, 2005
0

Re: Bitset

Quote originally posted by ramcfloyn ...
any ideas?
I'm not really sure, but it sort of sounds like you want to do something along this line...
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <bitset>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. int main()
  7. {
  8. static const char init[] = "abcdefghijklmnop...";
  9. std::vector<std::bitset<8> > str(init, init + sizeof init);
  10. std::copy(str.begin(), str.end(),
  11. std::ostream_iterator<std::bitset<8> > (std::cout, "\n"));
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. 01100001
  17. 01100010
  18. 01100011
  19. 01100100
  20. 01100101
  21. 01100110
  22. 01100111
  23. 01101000
  24. 01101001
  25. 01101010
  26. 01101011
  27. 01101100
  28. 01101101
  29. 01101110
  30. 01101111
  31. 01110000
  32. 00101110
  33. 00101110
  34. 00101110
  35. 00000000
  36. */
Which I think is saying that I believe you need to do the bitset differently.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 9th, 2005
0

Re: Bitset

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?!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ramcfloyn is offline Offline
10 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Help Please.....
Next Thread in C++ Forum Timeline: file query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC