c++ - Word variable when assigned to int type variable,it has ffff in front of it...why? -


short ab = 0x8400; word val = ab; int val1 = ab; cstring strmsg = _t(""); strmsg.format(_t("value of val1 = %x"), ab); afxmessagebox(strmsg); 

then ab has 0xffff8400 why? 0xffff come ?

short ab = 0x8400; 

assuming short 16 bits, value 0x8400 (decimal 33792) cannot represented short, have overflow. value stored in ab is, strictly speaking, implementation-defined. in practice, actual value stored 33792 - 216, or -31744.

word val = ab; 

assuming word typedef unsigned short (as on windows), converts value of ab, -31744, short unsigned short. result 33792, or 0x8400. irrelevant, since never use value of val.

int val1 = ab; 

i'll assume int typedef int. don't know why you'd use such typedef. if int same type int, there's no point in using int rather int. if int can possibly other int, it's horribly misleading name.

anyway, converts value of ab short int. since value, -31744, within range of int, conversion preserves value, ab == -31744.

strmsg.format(_t("value of val1 = %x"), ab); 

assuming strmsg.format similar printf, %x format specifier requires argument of type unsigned int. ab of type int (well, int, we're assuming it's same). treat contents of int object ab as if of type unsigned int. result 0xffff8400 i'd expect if int 32 bits , represented using two's-complement. if want see actual value of ab, use correct format specifier, %d.

the following answer original version of question. edit changed code , value of variable you're examining; code in question inconsistent results reported.

quick summary: a *doesn't have value 0xffff8400; you're displaying incorrectly.

according microsoft's documentation, word 16-bit unsigned type, typedef unsigned short.

word devid = 0x8400; 

this sets devid 0x8400, or 33792 in decimal. within range of values representable in word (0 65535), there's no problem far.

int = devid; 

assuming int 32 bits, value 33792 implicitly converted word int. since it's within range of value int, conversion keeps same value, a equal 33792, or 0x8400.

if examine a 32-bit quantity, using hexadecimal, should 0x00008400.

(if int 16 bits, conversion of 33792 word int will, strictly speaking, yield implementation-defined result. it's that result -31744. when viewed as unsigned 16-bit quantity, should 0x8400. it's unlikely you're using system 16-bit int. if are, please update question make clear.)

so why seeing incorrect value 0xffff8400? have no idea. can guess code use print value of a incorrect -- or code defines , initializes a isn't you've shown us.

if update question show how you're printing 0xffff8400, preferably part of short, self contained, correct (compilable), example, can help. otherwise can you're doing wrong.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -