我从图像文件读出一个字节,现在想分析这个字节中的各个比特,请问如何读取出来?例如我读出来的一个字节是f7,那么就应该是11110111。我想先取前三个比特,赋给一个整形的变量,那么这个变量的值就应该为7。请问如何实现?

解决方案 »

  1.   


    var
      a:Word;
      b:integer;
    begin
       a := $F7;
       b := a shr 5;
       caption := intToStr(b);
    end;
      

  2.   

    Shr 右移,Shl 左移。
    帮助上有详细说明
    he operations x shl y and x shr y shift the value of x to the left or right by y bits, which (if x is an unsigned integer) is equivalent to multiplying or dividing x by 2^y; the result is of the same type as x. For example, if N stores the value 01101 (decimal 13), then N shl 1 returns 11010 (decimal 26). Note that the value of y is interpreted modulo the size of the type of x. Thus for example, if x is an integer, x shl 40 is interpreted as x shl 8 because an integer is 32 bits and 40 mod 32 is 8.
      

  3.   

    >>那么请问我想取中间某一位,例如第5位的值呢?x shr 3 and 1