answersLogoWhite

0

What is the use of bit field in structure declaration?

Updated: 8/21/2019
User Avatar

Wiki User

9y ago

Best Answer

A bit field allows you to assign an arbitrary number of bits to a member.

As an example, suppose we wish to implement an R6000 PPN (physical page number). A PPN is a 32-bit value divided up as follows:

page frame number (22 bits)

unused (3 bits)

cache coherency algorithm (3 bits)

nonreachable (1 bit)

dirty (1 bit)

valid (1 bit)

global (1 bit)

While we could use bitwise logic to determine the state of each bit or group of bits within a 32-bit value, it's always good programming practice to express ideas directly in code. Therefore we want a structure that is exactly 32-bits in length with members that exactly match those of the PPN itself. To achieve this we need to use bit fields:

//

// All example code based upon Chapter 25.5.5 "Bitfields" of

// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup

//

struct PPN { // R6000 Physical Page Number

unsigned int PFN : 22; // Page Frame Number

int : 3; // unused

unsigned int CCA : 3; // Cache Coherency Algorithm

bool nonreachable : 1;

bool dirty : 1;

bool valid : 1;

bool global : 1;

};

Any consumer of this structure need be left in little doubt regarding its purpose. Moreover, the implementer's job is greatly simplified:

void part_of_VM_system (PPN& ppn)

{

if (ppn.dirty) { // contents changed

// copy to disc

ppn.dirty = 0 ;

}

}

We can also see how much simpler it is to extract information from a structure's bit fields compared to that of a 32-bit value:

int get_CCA (const PPN& ppn) {

return (int) ppn.CCA;

}

int get_CCA (unsigned int ppn) {

return (int) (ppn>>3)&0x7;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the use of bit field in structure declaration?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are bit fields What is the use of bit fields in a Structure declaration?

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields, and their width in bits can be explicitly declared. Gagandeep Singh Bitfields can only be declared inside a structure or a union, and allow you to specify some very small objects of a given number of bits in length. struct { /* field 4 bits wide */ unsigned field1 :4; /* * unnamed 3 bit field * unnamed fields allow for padding */ unsigned :3; /* * one-bit field * can only be 0 or -1 in two's complement! */ signed field2 :1; /* align next field on a storage unit */ unsigned :0; unsigned field3 :6; }full_of_fields; The main use of bitfields is either to allow tight packing of data or to be able to specify the fields within some externally produced data files.


Find at least two passages in the Declaration that use parallelism What is the effect of the parallel structure on the idea of the passage?

From pages 3 thru 29 are examples


What weaponds did the Declaration of Independence use?

the declaration of independence is a piece of paper. it did not use weapons.


What is a headcollar?

it a bit like a bridel but without the bit.you use it to lead the horse from the field and to tie it up when you want to groom it.


Use the Declaration of Independence in a sentence?

the declaration of independence was signed July 4th


When using Microsoft Access what is the default field size for a text field?

for 2003? i believe it is 255 characters. not positive though... (I use Access quite a bit, however, I am still not sure of that number)


What is a Stadium made from?

A combination of materials, just like any large building. Stadiums use steel for the structure, grass for the field, plastic for seating.


Does the Declaration of Independence use induction or deduction?

The Declaration of Independence uses DEDUCTIVE thinking


How do you use declaration in a senence?

Declaration is the act of declaring. The Kitten made his declaration that all cats must loved!!! Cats Rule!!!


In which document did Thomas Jefferson use the ideas from the Enlightenment?

The Declaration of Independence.. jrc


Is The ball rolled in the field or The ball rolled on the field correct?

"The ball rolled on the field" is correct. "In the field" suggests that the ball is physically inside something, like a container or enclosed area, rather than rolling freely on the surface.


What is a homologous structure?

A structure with similar shape but a different use