I want access bit from Byte array.for example consider array of 30 bytes in which I want to access 1 to 15 bits... How to access it. kindly help me.......
"it is not working Logic error is there in this program..."
Here's an obvious first error:
startByte=startByte/8;
Anyway, your code seems too complex. Here's a very generic example that uses bit masks to count off when to do source and destination buffer reads and writes:
unsigned char src_buf[8]; unsigned char dst_buf[8]; void bitcpy(unsigned char *dst, unsigned char *src, unsigned char start_bit, unsigned char n_bits) { unsigned char src_byte, src_mask; unsigned char dst_byte, dst_mask; src += start_bit / 8; src_mask = 0x80 >> (start_bit % 8); src_byte = *src; dst_mask = 0x80; dst_byte = 0; while (n_bits--) { if (src_byte & src_mask) { dst_byte |= dst_mask; } if ((src_mask >>= 1) == 0) { src_mask = 0x80; src_byte = *++src; } if ((dst_mask >>= 1) == 0) { dst_mask = 0x80; *dst++ = dst_byte; dst_byte = 0; } } if (dst_mask != 0) { *dst = dst_byte; } } void main(void) { src_buf[0] = 0xFA; src_buf[1] = 0x0A; src_buf[2] = 0x5B; bitcpy(dst_buf, src_buf, 4, 20); for (;;); }
if (dst_mask != 0) { *dst = dst_byte; }
To avoid an extra store when the number if bits is a multiple of 8, that should have been:
if (dst_mask != 0x80) { *dst = dst_byte; }
Sorry, my bad. Where are all you code reviewers anyway?