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, it was a nice

Love Story Part 3 [Materialistic]

Link to:  Part 2[Question] Jas and Gopi sat on the round shaped laptop desk at the Wi-Fi hut. All others took their seat in a normal fashion. Amishi started, “Does love is about something like friends with benefits or sex?”. As they have never discussed this topic among themselves before, everyone stood calmly. Yashwanth said, “Many feel like they are in need of love just to show off their greatness to their friends”. He added, “Many fall in love for 2 major reasons”.  1.They didn't get any sort of affection from their family and their close buddies  2. When they have number of arrears. “Most probably”, Ganga agreed. “Some love for time pass too... right”, Yashwanth said. Amishi didn't agree with Yash. She glanced at him. Their eyes met and none of them even blinked until Amishi  roke contact after few seconds. Things went for a long time. By the end, they came to a conclusion in unison. Love should not be materialistic. Love is not just for enjoyment and time pa