Hi ,


I need to Assign 4 bits to unsigned char:

I am using,

unsigned short Ver : 4 ;


My ques is ... howw do I initialize Ver ,

coz Ver=5 is not working..

If i need a type cast.. what will it be???

use a stucture, like this

struct num
{
    unsigned short v1: 4;
};

int main()
{
    num var;
    var.v1 = 5;
    printf("%d\n", var.v1);       
    return 0;

}
commented: Very clear +2

use a stucture, like this

struct num
{
    unsigned short v1: 4;
};
 
int main()
{
    num var;
    var.v1 = 5;
    printf("%d\n", var.v1);       
    return 0;
 
}

Actually I was using a function..

Here u go :


typedef struct {
int m ;
unsigned short usVer : 4 ;
unsigned short usIHL : 4 ;
unsigned short usTOS : 8 ;
unsigned short usTotLen ;
unsigned short usId ;
unsigned short usFlag : 3 ;
unsigned short usFragOff : 13;
unsigned short usTTL : 8 ;
unsigned short usProto : 8 ;
unsigned short usHeadChk ;
unsigned int uiSIP ;
unsigned int uiDIP ;
} IPHeader_format ;


IPHeader_format IPHeader1;


IPHeader1 = {1 , 4 , 5 , 0 , 1500 , 1 , 0 , 0 , 8 , 4 , 0 , 0 , 65535 };

do it all on one line

IPHeader_format IPHeader1 = {1 , 4 , 5 , 0 , 1500 , 1 , 0 , 0 , 8 , 4 , 0 , 0 , 65535};

Thanks .. it works..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.