Can anyone tell me what a bit field is in layman's terms? I am doing a PHP course in debugging and the instructor is using that term a lot. This is in the context of PHP error reporting.

Here is a quote from the transcript:

Error reporting sets the error reporting level with an integer representing a bit field normally through a named constant. By default, PHP reports everything except E_NOTICE and for versions prior to PHP 5.4, also excluding E_STRICT.

I think it's important as an aspiring programmer that I understand the nomenclature of my trade :)

Thanks for your help!

Note: I already made an attempt at wikipedia...

有帮助吗?

解决方案

We need to start out with what is a bit. A bit will take two values -- a zero or a one. By convention, a zero is also referred as false while a one is referred to as true.

a bit field is several bits.

I'll digress here by discribing to two common organization of bits -- characters and words.

In the old days at the time the pc came out characters where eight bits and words were 32 bits. Today, words are moving to 64 bit words. Characters are moving to 16 bits.

an integer representing a bit field

I do not know PHP how is dividing out the integer, but this is the idea. Basically, if you looked at the integer at the bit level you would find some patterns that have some meaning.

You need to understand hex and the powers of 2 to make more sense of this.

a 32 bit integer would be
0000 0000  0000 0000  0000 0000  0000 0000  

Putting in two bit fields...
0000 0000  0000 0000  0000 0000  AAAA BBBB 

Looking at the integer from the bits you could find two fields A & B each 4 bits wide.

AAAA might have the value of 1100 or C in hex BBBB might have the value of 0111 or 7 in hex.

the integer would have a value of C7 in hex or 199 in decimal.

So, the integer 199 when looked at with bit fields would have a different look.

Robert

其他提示

  • A bit field is a variable that consists of a specified number of bits.
  • A bit field can be a member of structure or a union.
  • A bit field is interpreted as an integral type.

    Syntax:

    struct structName
    {
      dataType identifierName1: numberOfBits;
      dataType identifierName2: numberOfBits;
      ...
    };
    
  • The ‘numberOfBits’ must be a nonnegative integer value.

  • Bit fields do not have addresses. There for arrays of bit fields, pointers to bit fields and functions returning bit fields are not possible.

  • The address-of operator (&) cannot be applied to bit-field

  • Unnamed bit fields can exists and they cannot be referenced.

  • The contents of unnamed bit fields at run time are unpredictable. They can be used as "dummy" fields, for alignment purposes.

  • An unnamed bit field whose width is specified as 0 guarantees that storage for the member following it in the declaration-list begins on an int boundary.

    typedef struct bitField
    {
      unsigned x  : 3;  // 0 to +7   (3 bits)
      signed      : 2;  // No name - 2 Bits of padding 
      signed y    : 6;  // -32 t0 +31  (6 bits)
      unsigned    : 0;  // Force alignment to next boundary.
      unsigned z  : 1;  // 0 or 1   
    } Temp;
    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top