Thoughts on the change and calculation of the number of 1 and 0 in the number of unsigned integers

Source: Internet
Author: User

The first thing you can do is to change the number of 1 in the unsigned int number to 0 and the calculation:

</pre><pre name= "code" class= "CPP" >/**************************************************************** * Copyright (c) 2015,wk Studios * * filename:a.h * * COMPILER:GCC VC 6.0 * * AUTHOR:WK * * TIME:2015 2 7 * **********************************************************************/#include <iostream> using namespace std; #include <assert.h>void set_bit (size_t * int_dat,size_t Posi,bool ch) {if (ch) {*int_dat |= (0x01 << (Posi-1));} Else{*int_dat &= ~ (0x01<< (Posi-1));}} size_t cal_bit1 (size_t d) {size_t N=0;while (d) {if (D & 0x01) {++n;} d/=2;} return n;} size_t cal_bit0 (size_t d) {size_t N=0;while (d) {if (! ( d%2)) {++n;} d/=2;} return n;} size_t cal_b1 (size_t d) {size_t N=0;while (d) {++n;d&= (d-1);//end one position 0}return N;} size_t cal_b0 (size_t d) {size_t n=0;while ((d+1)) {++n;d |= (d+1);//end one position 1}return N;} void Main () {cout<< "-----Computes the number of 0 or 1 in an unsigned integer-----\ n"; unsigned int m=15;cout<< "original data:" <<m<<endl; cout<< "0 Numbers:" << Cal_bit0 (m) <<endl;//This calculation ignores the front 0 cout<< "1 numbers:" <<cal_bit1 (m) <<endl;cout<< "0 Numbers:" < < cal_b0 (m) <<endl;//calculated in 32 bits of 0 cout<< "1 numbers:" << cal_b1 (m) <<endl;cout<< "----- unsigned integer specified position 0 or 1-----\ n "; unsigned int d=4;cout<<" original data: "<<d<<endl; Set_bit (&d, 2,1);cout<< "Specify position 1 after:" <<d<<endl; Set_bit (&d, 2,0);cout<< "specified position after 0:" &LT;&LT;D&LT;&LT;ENDL;}

Assign 1 to the position specified by the unsigned integer using the class implementation:

#include <iostream>using namespace std; #include <assert.h> #define Fsize 32#define TRUE 1#define FALSE 0class flags{unsigned Char F[fsize];p ublic:flags (); void set (int i); void clear (int i); int read (int i); int size ();}; Flags::flags () {memset (f,false,fsize);//initialized to fsize bits are all 0}void flags::set (int i) {assert (i>=0 && i<fsize); F[i]=true;} void flags::clear (int i) {    assert (i>=0 && i<fsize); f[i]=false;} int flags::read (int i) {assert (i>=0 && i<fsize); return f[i];} int Flags::size () {return fsize;} void Main () {flags f1;for (int i=0;i<f1.size (); ++i) {if (i%3==0)//is assigned as 1f1.set (i) in a position that can be divisible by three;} for (int j=0;j<f1.size (); ++j) {Cout<<f1.read (j) << "";} Cout<<endl;}

more complex implementations of bits modifications to unsigned integers :

/**********************************************************************     * *   Copyright (c) 2015,wk studios   * *   filename:  A.h * *   compiler:gcc  VC 6.0&NB sp;   * *   author:wk     * *   time:2015 2 7 * ********************** /#include <iostream>using namespace std; #include < assert.h> #include <limits.h>const unsigned char highbit=1 << (char_bit-1); class bitvector{public: Bitvector (); Bitvector (unsigned char* init,int size=8); Bitvector (char* Binary);//can use the "000101010" string to initialize ~bitvector (); void set (int init); void clear (int bit); int read (int bit) , void bits (int sz), int bits (), void print (const char* msg= ""), or the default constant string is still stored in the constant area private:unsigned char* bytes;int bits, numbytes;//bits and bytes}; Bitvector::bitvector ()//Assign all variables to 0 {numbytes=0; bits=0; Bytes=0;} Bitvector::bitvector (unsigned char* init,int size)//Allocate memory andNumber of initialization bits {numbytes=size;//bytes 11110000bits=numbytes*char_bit;//total number of bits bytes= (unsigned char*) calloc (numbytes,1);// void *calloc (size_t n, size_t size);//Allocate numbytes 1-byte space assert (Bytes); if (init==null) {return;} for (int index=0;index<numbytes;++index) {for (int offset=0;offset<char_bit;++offset) {if (Init[index] & (            Highbit>>offset)) Set (index * char_bit+offset);}    }}bitvector::bitvector (char* binary) {//"1111010010" assert (binary);    Bits=strlen (Binary); numbytes=bits/char_bit;//Bytes//Less than 8 bit is also assigned a byte if (bits%char_bit) numbytes++;      bytes= (unsigned char*) calloc (numbytes,1), assert (Bytes), for (int i=0;i<bits;++i) {if (binary[i]== ' 1 ') set (i);} }bitvector::~bitvector () {free (Bytes);}   void Bitvector::set (int bit) {assert (bit>=0 && bit<bits);   int index=bit/char_bit;   int offset=bit% Char_bit;   unsigned char mask = (1<<offset); Bytes[index] |= mask; } int bitveCtor::read (int bit) {assert (bit>=0 && bit<bits); int index=bit/char_bit;   int offset=bit% Char_bit;   unsigned char mask = (1<<offset); Return Bytes[index] & mask;//can actually only use one sentence to end the problem, but the program is too readable}void bitvector::clear (int bit) {assert (bit>=0 &&    bit<bits); int index=bit/char_bit;    int offset=bit% Char_bit;    unsigned char mask =~ (1<<offset);   Bytes[index] &= mask; }int bitvector:: Bits () {return bits;} void bitvector::bits (int size) {int oldsize =bits; Bits=size; Numbytes=bits/char_bit; if (bits%char_bit) numbytes++; void* V=realloc (bytes,numbytes); ASSERT (V); bytes= (unsigned char*) V; for (int i=oldsize;i<bits;++i) {clear (i);}} void Bitvector::p rint (const char* msg) {puts (msg); for (int i=0;i<bits;i++) {if (read (i)) Putchar (' 1 '); Elseputchar (' 0 '); if ((i+1)%char_bit = = 0) Putchar (");} Putchar (' \ n ');} void Main () {unsigned char b[]={0x0f,0xff,0xf0, 0xaa,0x78,0x11};//unsigned char b[]={' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; Bitvector Bv1 (b,sizeof(b)/sizeof (*b)), Bv2 ("10010100111100101010001010010010101"); Bv1.print ("Bv1 before Modification"); for (int i=36;i <bv1.bits (); ++i) {bv1.clear (i);} Bv1.print ("Bv1 after Modification"), Bv2.print ("Bv2 after Modification"), for (int j=bv2.bits () -10;j<bv2.bits (); ++j ) {bv2.clear (j);} Bv2.set (Bv2.print) ("Bv2 after Modification"), Bv2.bits (Bv2.bits ()/2), Bv2.print ("Bv2 cut in Half"); Bv2.bits (    Bv2.bits () +10); Bv2.print ("Bv2 grown by 10"); Bitvector Bv3 ((unsigned char*) 0);}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Thoughts on the change and calculation of the number of 1 and 0 in the number of unsigned integers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.