python - Simple operations with TCP/IP Header bits -
python - Simple operations with TCP/IP Header bits -
i have 2 doubts related operations bits:
the first 1 >> operator. far have read, operator this
0001 0000 >> 4 --> 0000 0001 or 1111 0001, not sure if fills 1s or 0s
so have code:
version_ihl = iph[0] version = version_ihl >> 4 ihl = version_ihl & 0xf
let's ip version 4: 0100 , ihl 5: 0101
if version = version_ihl >> 4, result be
0100 0101 ---> 0000 0100 in case operator >> doesn't fill 1, have read does, instead of ip version = 4 1111 0100 not 4 @ all.
the sec thing line, how calculate ihl ihl = version_ihl & 0xf
the final result 5, 0000 0101 , far know, operator mask of 1s, 0100 0101 remain same. 2 questions :
1.how obtain 0100 0101 >> 4 operation result 0000 0100 if >> supposed fill 1s¿?
2.how obtain 0100 0101 result 0000 0101 & 0xf mask¿?
the code posted worked right, solution there, want know why works that. give thanks in advance, hope through lite on this.
the >>
operator not "fill" anything. integers in python behave though have infinite number of leading zeros (nonnegative integers) or ones (negative integers). right-shifting drops n to the lowest degree important bits. 0xf
same 0b1111
, in turn same 0b00001111
. masking to the lowest degree important nybble ones.
since asked more detail:
you started0100 0101
, right-shifted four. that's exactly equivalent starting 0000 0100 0101
, right-shifting four, gives 0000 0100
. that, in turn, same 0100
. you started 0100 0101
, masked 0xf
. 0xf
1111
, same 0000 1111
. 0000 0101
, or 0101
python bit-manipulation bit-shift tcp-ip raw-sockets
Comments
Post a Comment