有一段数据
void *pData;
我如何去得到一个bit的值

解决方案 »

  1.   

    如何得到某一位的值(这一位距数据首位距离为n个bit)
      

  2.   

    因为不知道你的void *到底指向什么类型的数据,你需要先转换一下,得到一个char*指针,然后进行按位与运算就行了
    -------------------------
    char *pchar=(char*)pData;
    //假设得到距离首位的n位的bit,首位为0位基数
    char *begin=(pchar+n/8);//得到起始字节地址
    char mask=128>>(n%8);
    char ch=(*begin)&mask;
      

  3.   

    psusong的方法就可以,我想API索然语句简单,但是实际执行起来还是要用到具体的语句,可能速度更慢
      

  4.   

    我重新写了一个
    ----------------------------
    // Shift.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include "iostream.h"
    struct ExtentInt
    {
    int low;
    int high; 
    };int main(int argc, char* argv[])
    {
     int x,y;
     ExtentInt temp;
    cout<<"Please input the high number:";
    cin>>x;
    cout<<endl;
    cout<<"Please input the low number:";
    cin >>y; temp.high=x;
    temp.low=y; char *pchar=(char*)&temp;
     int size=sizeof(ExtentInt)*8; for(int i=size-1;i>-1;i--)//因为结构体从低位存起
    {
    char *begin=(pchar+i/8);//得到起始字节地址
    char mask=1<<(i%8);
    char ch=(*begin)&mask;
    while(ch>1)
    ch/=2;//convert it to int :0,1
    cout <<size-i <<" bit is : "<<int(ch)<<endl;
    } return 0;
    }
      

  5.   

    更正:因该使用unsigned char 否则会出现-128
    ---------------
    // Shift.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include "iostream.h"
    struct ExtentInt
    {
    int low;
    int high; 
    };int main(int argc, char* argv[])
    {
     int x,y;
     ExtentInt temp;
    cout<<"Please input the high number:";
    cin>>x;
    cout<<endl;
    cout<<"Please input the low number:";
    cin >>y; temp.high=x;
    temp.low=y; unsigned char *pchar=(unsigned char*)&temp;
     int size=sizeof(ExtentInt)*8; for(int i=size-1;i>-1;i--)//因为结构体从低位存起
    {
    unsigned char *begin=(pchar+i/8);//得到起始字节地址
    unsigned char mask=1<<(i%8);
    unsigned char ch=(*begin)&mask;
    while(ch>1)
    ch/=2;//convert it to int :0,1
    cout <<size-i <<" bit is : "<<int(ch)<<endl;
    } return 0;
    }