I have a block of memory allocated 20bytes(160-bits) with memset value of 1. Each bit represents an incoming data, if the data is received the bit is set else reset. I have initially set all the 160-bits and I will reset if the data is not received. Below is the sample code:
char *buf = malloc(20);
memset(buf,1,20);
recvfun() {
static int index;
index++;
if(!received)
*buf = *buf ^ (1<<(160-index));
...
}
I think *buf will give only 8-bits, not the complete memory block, so everytime I try to reset the bit, the above code only resets in the first 8-bits. If suppose a 99th data is not received I need to reset 99th bit. Can you please help me in achieving this. thanks for your valuable time.
You need to break it down into a byte index and a bit index, e.g. change:
to:
Note also that
memset(buf,1,20);in your code above should bememset(buf,255,20);if you want to initialise all bits to 1.