i am looking at the CoAP implementation in ContikiOS, particularly at the header parsing and i am having some trouble understanding two operations. The code is bellow.
coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer)
coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer)
Basically this shifts both of the values down to the least significant bits.
The version bits are at the most significant bits, let's call them bits 7 and 6. So, the four possible values with the mask applied are 0xC0, 0x80, 0x40 and 0x00. For later use, e.g. for version comparisons, changing the range to 3, 2, 1 and 0 might be more useful, so shifting by six bits moves the value to bits 1 and 0. (In fact the mask is unnecessary in this case because the shifting eliminates all but the most significant two bits.)
It's the same story with the type bits, but those are bits 5 and 4, giving you 0x30, 0x20, 0x10 and 0x00 after applying the mask. Shifting by four bits moves those bits to 1 and 0.