开发者

warnings in playing with bits

开发者 https://www.devze.com 2023-02-25 02:50 出处:网络
BYTE original = 0xF0F0; BYTE bMask = 0x0000; BYTE wMask = 0xFFFF; BYTEnewBits = 0xAAAA; /*& operation with \"0bit set 0\" & \"1bit give no change to original byte\" */
 BYTE original = 0xF0F0;
     BYTE bMask = 0x0000;
BYTE wMask = 0xFFFF;
BYTE  newBits = 0xAAAA;

/*& operation with "0bit set 0" & "1bit give no change to original byte" */
cout<<"Original o: "<<bits(original)<<endl;
cout<<"NewBits: "<<bits(newBits)<<endl;
cout<<"BMask m: "<<bits(bMask)<<endl;
cout<<"WMask m: "<<bits(wMask)<<endl;
cout<<"o & m with BMask: "<<bits(original & bMask)<<endl;/*0 set original bit as 0 */
cout<<"o & m with WMak: "<<bits(original & wMask)<<endl;/*1 bit put no effect on image*/开发者_开发百科
cout<<"Result"<<bits(original & wMask | newBits)<<endl;

My OutPut is right but i am getting warnings...

first i did declaration with char type because char take 1Byte in memory.. but still that give me warning,...

Then i apply BYTE byte instead of char...because BYTE also take 1Byte in memory..

Warnings: warning C4309: 'initializing' : truncation of constant value

this warning is on all declaration lines.. If char and Byte take 1Byte in memory then why i am getting warning.. what am i missing here? Can any one help me.. Expecting a good response Thanks


E.g. 0xF0F0 requires 16 bits - 2 bytes. Try with an unsigned short instead of BYTE


1 Byte = 8 Bits. 0xf0f0 is 16 Bits.


0xf0f0, 0xffff and 0xaaaa are two bytes constant, a short int in other words.

The compiler is just warning you that it is throwing away the most significant bytes of the two you are assigning:

0xf0f0 => compiler assigns 0xf0;
0xffff => compiler assigns 0xff;
0xaaaa => compiler assigns 0xaa;

The question is: why do you want to assign a 2-byte constant to something you expect to be 1-byte sized?

0

精彩评论

暂无评论...
验证码 换一张
取 消