Thread: help me

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12

    help me

    i need a bit stuffing example

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In what context?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    i want to simulate bit stuffing in frames

    the flag is "01111110"

    and i must do bit stuffing after the 5th bit

    then un-stuffing it in the reciver

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So we are talking HDLC comms, yes?

    The problem with bitstuffing in C is that the language is not "bit oriented". It makes life quite difficult.

    I take it this is an assignment of some sort, so you really should post your code first, then we can help you "improve it".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    the code attached

    please check the stuffing and the unstuffing function

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does this code work for you? I don't see anything directly wrong, but I can't say for sure if it works for all cases or not.

    Code:
    pow(double(2),((max-1)-t))
    is the same as
    Code:
    1 << ((max - 1)-t)
    The latter doesn't use "math.h" and is much quicker.

    Code:
        int data[2084];
    Did you mean 2048? If you use constants, it would be much easier to avoid typos like this [not that it REALLY makes any difference in this case - but still].

    Code:
     dataCount=dataCount-2;
    Use:
    Code:
     dataCount -= 2;
    I just tried your code, typing in a few ~ characters [which is 01111110 in binary], and it didn't produce the same output as I input. It is hard to produce sequences that have more than 5 consecutive bits, since most such characters are unreachable from a normal keyboard.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    thanks for the effort

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    To turn your byte array into a bit array, try something like this
    Code:
    void writeBit ( unsigned char *array, int bitPosition, unsigned char bit ) {
      int bytePos = bitPosition / 8;        // or CHAR_BIT to be precise
      int bitPos = bitPosition % 8;
      unsigned char mask = ~(1 << bitPos);  // 11110111 with the 0 in the right place
      bit <<= bitPos;                       // the bit we want to set, in the right place, eg 00001000
      array[bytePos] &= mask;               // clear existing bit value
      array[bytePos] |= bit;                // set new bit value
    }
    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.

Popular pages Recent additions subscribe to a feed