Skip to main content

bitset C++

<bitset>

What is the size of char datatype in C++?
What is the size of bool datatype in C++?
Both take 1byte to store a value. Yes, bool datatype also takes 1byte.

Bitset is a standard library available in c++ which allows us to store data in bitwise. The good thing was we can access each bit separately in a bitset as like elements in an array. Only change was we access them from the backside.

Eg: g = 00111011
then g[0]=g[1]=g[3]=g[4]=g[5]=1
and g[2]=g[6]=g[7]=0

It can be constructed[accepts] from Integer and Binary strings only. How many lines of code you write to convert a decimal to binary? .... It can be done in just 2 lines using the bitset.

#include <bitset>
#include <iostream>
using namespace std;
int main()
{
  bitset<8> vin;
  //here 'vin' will have all 8 bits unset as zero
  // Value 8 inside angle brace denotes number of bits it can also be 16 or 32

  bitset<8> tam(5);
  //It will store number 5 as a bit stream in 'tam'

  cout << vin.any() << endl;
  cout << tam.any() << endl;
  //It chk wheather there is atleast one 1 in the number; if so return 1 else 0

  cout<<vin.all()<<endl;
  cout<<tam.all()<<endl;
  //It chk wheather all bits are set as 1; if so return 1 else 0

  cout<<vin.none()<<endl;
  //It returns true, if none of the bit is set as 1

  cout<<"Flipping all :"<<tam.flip()<<endl;
  //All bits will be fliped

  cout<<"Flipping 3rd bit alone :"<<tam.flip(3)<<endl;
  //3rd bit alone fliped
  //changes are made permanent in source

  cout<<"Set all :"<<vin.set()<<endl;
  cout<<"Reset all :"<<vin.reset()<<endl;
  cout<<"Set 5th bit alone :"<<vin.set(5)<<endl;
  cout<<"Reset 5th bit alone :"<<vin.reset(5)<<endl;

  cout<<"count 1's in bitset :"<<vin.count() <<endl;
  //count returns number of 1 bits in bitset

  cout<<"Size of inb :"<<vin.size()<<endl;
  //no. of bits in vin

  cout<<"count 0's in bitset :"<<vin.size() -vin.count() <<endl;
  //Subtracting the count of 1's from total bits

  cout<<"Testing wheather 3rd bit is set :"<<tam.test(3)<<endl;
  //returns 1 if it is set else 0

  return 0;
}

Similarly,
vin.to_ulong() converts bitset to unsigned long integer.
vin.to_ullong() converts bitset to unsigned long long integer.
vin.to_string() converts bitset to unsigned long long integer.

Done.

Comments

Popular posts from this blog

குட்டி ஆசை - அத்தா அம்மா

பழைய நிகழ்வுகளே காரணம் என நினைக்குறேன், அப்பாவின் ஜீப் மோகத்திற்கு. சிகப்பு நிறத்தில் ஒரு ஜீப் அதை அப்படியே அப்பாவிற்கு அர்ப்பணிக்க வேண்டும்.  என் சிறுவயதில் அவள் அதிகமா திரில்லர் நாவல்கள் படிப்பதை கவனித்திருக்கிறேன், தற்சமயம் அவள் அதிலிருந்து வெகு தூரம் சென்றுவிட்டாலோ? என்னசெய்தாயினும் அம்மாவை புத்தக வாசிப்பிற்க்கு  மீண்டும் கொண்டு வர வேண்டும்.  இசை ஜாம்பவான்களின் நிகழ்ச்சிகளுக்கு, அப்பாவை அழைத்துச் செல்ல வேண்டும். முக்கியமாக SP பாலசுப்ரமணியம் கச்சேரிகளுக்கு. முறையான இசை ஞானத்தை அடைய உதவ வேண்டும், கச்சேரிகள் நடத்தவும் உறுதுணையாக இருக்கனும். நேரத்தை பிரித்து, உடற்பயிற்சி செய்ய ஏற்பாடு செய்து தரனும். இருவரையும் ஜோடியாக உடற்பயிற்சி செய்ய வைத்தால் நன்றாக இருக்கும். ஒருவரை ஒருவர் மெச்சிக் கொள்வார்கள் தான்.  குதிரை வளர்ப்பில் ஏனோ ஒரு பிரியம், நல்ல திடகாத்திரமான அரேபிய குதிரையை அவர்கள் வளர்ப்பதை ரசித்திடவேண்டும்.  நல்ல உடல்நிலையை உறுதி செய்ய இருவருக்கும் வருடம் இருமுறை முழு உடல் பரிசோதனை செய்யனும். முடியும் என்றால், ஸ்டெம்செல் த...

Aiii We | Industrial Visit (IV)

Eighth-semester bucket list was filled with four important works, Mathrix symposium - This is where the rubber meets the road Freshers day -  Made it, on the same day we had Mathrix workshop Farewell for seniors - Cloned copy available in hand Industrial visit   Almost everyone in the class was involved somehow in one of these activities, planning for the so-called Industrial visit started well ahead of time at Dec’18. Mahesh took the responsibilities in the shoulder, Rukmani and Akshaya stood as long pillars from the girl's side.   Square one   Feb 7, Out of 6 hours of class we canceled the last 3 hours. Most of the day-scholars went back and joined in central, hostellers kicked off their journey to Central from the yellow bus of CEG standing up against the alumni center. Vineeth and co placed few lemons for a safe journey and fire on a coconut wishing the same. At central, most of the day-scholar girls brought their parents, few boys too. Yes,...

Our first Interview

To start of with, we were asked about 20 aptitude questions and to write essay. Later 11 of us got selected and proceeded for next round. They almost had their aptitude results ready in 15 minutes. Initially I was very nervous but interviewers attitude towards me comforted. The interview started with a smile and hi Tell me about yourself? Explain about project? Explain project by sketching in paper. About the techofes events I have been part of. Find the second largest element among the repeating elements in an array. Example: 2 5 5 3 3 3 1 Output:3 What do you know about thorogood? According to you what do you think you must improve and how are you going to do that? What was the help you did apart from you must do kind of things? They first ask me you have been waiting from long time how do u feel first I didn't hear properly I said sorry they asked me again and I said its good I went to hostel wid my Frnds then usual questi...