The application of the C-language bit segment: The number of bits in the statistics binary

Source: Internet
Author: User

A bit segment is a special struct that is used less in the C language, and the C language allows you to specify the memory length of its members in bits in a struct, which is called a bit field or bit field in bits. Use bit segments to store data with fewer digits.

the access to information is generally measured in bytes. In fact, sometimes storing a message does not have to use one or more bytes, for example, "true" or "false" with 0 or 1, just 1 bits. When the computer is used in the field of Process control, parameter detection or data communication, control information often occupies only one or several bits in one byte, often putting a few information in one byte. How to assign and change the value of one or several bits in a byte? There are two ways to do this:(1) An integer variable data can be artificially divided into several parts. but it's too cumbersome to assign a certain number of bits in a byte in this way. A method that can segment the structure body.
struct packed-data{          unsigned a:2;          unsigned b:6;          unsigned c:4;          unsigned d:4;          int i;} Data
The structure of its memory allocation is as follows


The statistical information of the data can be realized by using the bit segment structure.

Reference title: Number of 1

Topic analysis, the subject is clearly asked to count the number of input integers 1, of course, the problem can also use bit operations to statistics, here, we use a bit segment for the knot.

It is known that the input data n<=1000<2^16, so the design of 16-bit structural posture segment

typedef struct A{UNSIGNED Short a0:1, unsigned short a1:1, unsigned short a2:1, unsigned short a3:1, unsigned short a4:1;< c5/>unsigned short a5:1; unsigned short a6:1; unsigned short a7:1; unsigned short a8:1; unsigned short a9:1; unsigned short a10:1; unsigned short a11:1; unsigned short a12:1; unsigned short a13:1; unsigned short a14:1; unsigned short a15:1; }datatype;

take a name for each bit AI 0<=i<16

Then, to find a way to convert the whole number into a paragraph, the direct assignment is definitely wrong, using each bit assignment, requires in-place operation, complex and error-prone, here, we want to use a memory copy function memcpy (void*,void*,size_t)

In fact, whether it is a struct, an array, or an int variable, in the final analysis or continuous representation in memory, the memcpy function can achieve a copy of the data, based on the memory level.

Reference Source:

#include <memory.h> #include <iostream>using namespace std; #define  sums (b) b.a0+b.a1+b.a2+b.a3+b.a4+ b.a5+b.a6+b.a7+b.a8+b.a9+b.a10+b.a11+b.a12+b.a13+b.a14+b.a15typedef struct a{unsigned short a0:1, unsigned short a1:1 ; unsigned short a2:1; unsigned short a3:1; unsigned short a4:1;  unsigned short a5:1; unsigned short a6:1; unsigned short a7:1; unsigned short a8:1; unsigned short a9:1; unsigned short a10:1; unsigned short a11:1; unsigned short a12:1; unsigned short a13:1; unsigned short a14:1; unsigned short a15:1; }datatype;int Main (int argc, char** argv) {DATATYPE p;int c;int n;//scanf ("%d", &n); Cin>>n;while (n) {unsigned Short b;n--;cin>>b;memcpy (&p,&b,sizeof); C=sums (P); Cout<<c<<endl;} return 0;}


The application of the C-language bit segment: The number of bits in the statistics binary

Related Article

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.