Thread: Parity check

  1. #16
    .
    Join Date
    Nov 2003
    Posts
    307
    I disagree. You're stating standards I assume. Can you cite something specific, please?

    Most CS classes teach this with the usual caveats about overflowing, and the issues of portability like endian-ness.

    http://www.cs.cf.ac.uk/Dave/C/node13.html

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Well that's a woefully inadequate description of bit-fields. The examples are pretty much guaranteed to only work on the (unspecified) implementation which the author tried them on.

    Code:
    struct bmap{              /* bitfields for 8 bit byte  */
            unsigned bit7:1;  /* 8 bit char  */
            unsigned bit6:1;
            unsigned bit5:1;
            unsigned bit4:1;
            unsigned bit3:1;
            unsigned bit2:1;
            unsigned bit1:1;
            unsigned bit0:1;
    };
    int main ( void ) {
        printf( "%ld\n", sizeof(struct bmap) );
        return 0;
    }
    First compiler I tried, it printed 4

    If you want standards, try this
    Section 6.7.2 of the free one
    Code:
           [#9] An implementation may allocate any addressable  storage
           unit  large  enough  to  hold  a bit-field.  If enough space
           remains, a bit-field that immediately follows  another  bit-
           field  in  a structure shall be packed into adjacent bits of
           the same unit.  If insufficient  space  remains,  whether  a
           bit-field  that  does  not  fit is put into the next unit or
           overlaps  adjacent  units  is  implementation-defined.   The
           order  of allocation of bit-fields within a unit (high-order
           to low-order or low-order to high-order) is  implementation-
           defined.   The  alignment of the addressable storage unit is
           unspecified.
    It only has to be large enough - there is nothing about being exactly small enough.

    If you want small, then you have to use implementation specific "pack" pragmas, attributes or whatever else your compiler provides.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    Quote Originally Posted by Prelude
    Code:
    unsigned int
    bits(
      unsigned int val
      )
    {
      int n = 0;
    
      while (val) {
        ++n;
        val &= val - 1;
      }
    
      return n;
    }
    I like this one very much... I would never have thought of doing it that way.

    I'll try out the different alternatives anyway. Thanks to all for your suggestions.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by quzah
    You could have just compiled it. I was going to do this...
    Code:
    unsigned char OxOO( unsigned int Ox0O )
    {
            int OxO0,Ox00;
            for( OxO0=Ox00=0x00;OxO0<sizeof(unsigned int)*CHAR_BIT;OxO0=OxO0+0x01 )
                    Ox00=Ox00+(!!(Ox0O&(0x01<<OxO0)));
            return Ox00;
    }


    Quzah.
    I love it!!! Here compile this. Just don't try to read it.

  5. #20
    .
    Join Date
    Nov 2003
    Posts
    307
    I disagree. Here's why - this is from the section on object representation:
    (page 37)
    6.2.6 Representations of types
    6.2.6.1 General

    1 The representations of all types are unspecified except as stated in this subclause.
    2 Except for bit-fields, objects are composed of contiguous sequences of one or more bytes,
    the number, order, and encoding of which are either explicitly specified or
    implementation-defined.
    3 Values stored in unsigned bit-fields and objects of type unsigned char shall be
    represented using a pure binary notation.40)
    4 Values stored in non-bit-field objects of any other object type consist of n ´ CHAR_BIT
    bits, where n is the size of an object of that type, in bytes. The value may be copied into
    an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is
    called the object representation of the value. Values stored in bit-fields consist of m bits,
    where m is the size specified for the bit-field. The object representation is the set of m
    bits the bit-field comprises in the addressable storage unit holding it. Tw o values (other
    than NaNs) with the same object representation compare equal, but values that compare
    equal may have different object representations.
    This is from ISO/IEC 9899:1999 document (I do not know how to cite these documents), available as a pdf from http://www.nirvani.net/docs/ansi_c.pdf -

    What you are saying is there is no guarantee of size, when this states that for UNSIGNED CHAR in bit fields there is. It is CHAR_BIT or SCHAR_BIT which is defined as:
    (in the discussion of limits.h)

    number of bits for smallest object that is not a bit-field (byte) 8
    CHAR_BIT
    You did notice the "8" bits there. And the fact that it requires binary representation for bit fields. I do not see how your position is tenable. Sorry. Please explain how a binary representation (bit for bit) is not guaranteed to be the same size as UNSIGNED CHAR, taken in this context?

    I apologize for my failure to see your point of view, but I believe you are in fact incorrect.
    It is possible that we are 'talking past' one another.

    My point: bit fields up to one 8 bit byte are binary, bit-for-bit for the data they purport to represent. Period. You are saying, as I seem to get it, that because they are in a struct or a union that may possibly be larger than one 8 bit byte there is no such guarantee. In this instance we are dealing with 8 (CHAR_BIT) bit data objects.

    Correct me where I am wrong.

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    The size of a char (in terms of the number of bits) doesn't matter, since it has no bearing on the underlying "storage unit" in which bit-fields are stored (that's the quote that I posted), so long as that underlying "storage unit" is big enough to hold those bits.

    Sure the bits in your structure occupy 8 consecutive bits of a bit-field "storage unit" (because unsigned char must have at least 8 bits - your argument), that much is guaranteed at least (that's the adjacency rule in my quote).
    But there's nothing to stop an implementation stuffing those bits in a 32 bit integer (as my implementation does). Whether they occupy bits 0..7 or bits 24..31 is again implementation specific, so your chances of success are only 50%.

    That's what "any addressable storage unit" means. An unsigned int is an addressable storage unit, so my implementation is free to choose it.
    By the way, gcc and vc++ both give the answer 4
    Care to chip in with some compilers which give the answer 1 without resorting to even more implementation specific tweaks.

    The only thing your argument proves is that given a "unsigned bit:1" is that it will have the values 0 or 1 (the representation of bit-fields will be pure binary). It says nothing about the underlying storage unit used to store those bits. It certainly has no bearing on the number of padding bits which an implementation can add, so long as the adjacency rule is followed.

    Besides which, since it has already been pointed out by others, CHAR_BIT is only required to be at LEAST 8, not exactly 8.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    hello guys...i try to make an application, i have to make to communicate 2 devices together and send packets to each other...the problem is i cant manage to develop the bit stuffing problem...can anyone help me with that?

    bit stuffing: one counter will count the bytes and if check that 5 bits are "1" in row then it puts one 0 after those 5 "1" bits...

    i want that program work in c, thanks a lot

  8. #23
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Please don't dig up old threads, just create a new one specific to your problem and with more details.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Parity Check Matrix
    By Ken JS in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2007, 03:51 AM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM