You are here:

C++/use of bitset c++ URGENT

Advertisement


Question
my question is
1)how can i use <bitset> to store 10bit data

2)how can a 10 bit data stored in bitset can be stored in any other variable

i m asking this becoz i need to send 10 bit data to another computer
please help me with giving example codes

Answer
1/ Storing 10 bit data in a bitset

Well the obvious way is to specify 10 as the number of bits in your bitset type:

       #include <bitset>

       // ...

       typedef std::bitset<10>        SetOfTenBitsType;


       // ...

       SetOfTenBitsType tenBitWord(999); //999 stored as 10 bit bitset

Note that I used a typedef for convenience and clarity, you could of course just used std::bitset<10> directly - however 10 then becomes more of an embedded magic number, which would most likely make maintenance more troublesome if for example the word size changed to 12 bits in the future. So maybe the value 10 should be named:

       const size_t SpecialWordSize( 10 );

I used SpecialWordSize as I do not know what or why you are using 10 bit words, I expect you can think of a more appropriate name for the word size. Note that size_t is the bitset template parameter type, so maybe the following would be better:

       #include <bitset>

       // ...

       size_t const          SpecialWordSize( 10 );
       typedef std::bitset<SpecialWordSize>  SetOfTenBitsType;

       // ...

       SetOfTenBitsType tenBitWord(999); //999 stored as 10 bit bitset          

2/ Storing bitset values in objects of other types

The std::bitset class template provides for conversion to values of two other types: std::string and unsigned long via the to_string and to_ulong member functions. The to_string function returns the bitset value as a sequence of '0' and '1' characters. Here is an example of each:

       unsigned long tenBitAsULong( tenBitWord.to_ulong() );
       std::string tenBitAsString( tenBitWord.to_string() );

Once you have the value as an unsigned long you should be able to convert to any other numeric type - albeit with the potential to loose information if you try to squeeze the value into an 8-bit char type. An obvious choice would be a 16-bit type, maybe an unsigned short, as it is nearer to the number of bits you are using:

       unsigned short tenBitAsUShort( static_cast<unsigned short>
         ( tenBitWord.to_ulong() )
         );

Note the use of static_cast to keep the compiler quiet about warning of potential loss of data when converting from a long to a short. I wrapped the line as it was too long when pasted into my text editor. I expect the indentation to look a bit wrong if this answer is viewed using a proportionally spaced font. Try using a fixed space font such as Courier.

If you wish to perform any other conversions - maybe to and from your own types - then you will have to define you own conversion operations.

Here is a stand alone program putting together all the points mentioned in my answer. It was built using MSVC++ 8 (.NET 2005):

#include <bitset>
#include <string>
#include <iostream>

namespace
{
       size_t const          SpecialWordSize( 10 );
       typedef std::bitset<SpecialWordSize>    SetOfTenBitsType;
}

int main()
{
       SetOfTenBitsType tenBitWord(999); //999 stored as 10 bit bitset

       unsigned long tenBitAsULong( tenBitWord.to_ulong() );
       std::string tenBitAsString( tenBitWord.to_string() );
       unsigned short tenBitAsUShort( static_cast<unsigned short>
         ( tenBitWord.to_ulong() )
         );

       std::cout <<   "    tenBitWord: " << tenBitWord
         << "\n tenBitAsULong: " << tenBitAsULong
         << "\ntenBitAsString: " << tenBitAsString
         << "\ntenBitAsUShort: " << tenBitAsUShort
         << '\n'
         ;
}

I suggest you get access to a good reference on the C++ standard library. A good tutorial and reference is: "The C++ Standard Library A Tutorial and Reference" by Nicolai M. Josuttis. There is some online reference material at http://www.sgi.com/tech/stl/.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.