提取32位长的中间16位

Ash*_*ari 3 c++ bit-manipulation

我正在阅读Stroustrup撰写的TCPPPL.它给出了一个函数示例,它提取32位长的中间 16位,如下所示:

unsigned short middle(long a){ return (a>>8)&0xffff;}.
Run Code Online (Sandbox Code Playgroud)

我的问题是:是不是提取了最后 16位?告诉我,我错了.

YSC*_*YSC 6

它确实提取了中间的16位:

//    a := 0b xxxx xxxx 1111 1111 1111 1111 xxxx xxxx 
a>>8;   // 0b 0000 0000 xxxx xxxx 1111 1111  1111 1111
&0xffff // 0b 0000 0000 0000 0000 1111 1111  1111 1111
Run Code Online (Sandbox Code Playgroud)