I was reading a book that implement a do it yourself DNS message reader and it tries to see if a particular field is set true. One piece of code the book uses I can't understand well.
const int qdcount = (msg[10] << 8) + msg[11];
msg
is ==> char
type (i.e 8 bits)
qdcount
==> is supposed to be a field of 16 bits contains number of DNS queries ( made of 2 fields together msg[10]
and msg[11]
)
so how does this code work (if msg[10] = 01001 0001 for example) left shifting it by 8 is supposed to result (1000 0000) i.e UB, then any calculations done will result in a wrong answer. Suppose msg[11] = 0010 1111
. result of calculation is 1000 0000 + 0010 1111 right?. so how this line of code works exactly.