I think that this is not possible because Int32
has 1 bit sign and have 31 bit of numeric information and Int16 has 1 bit sign and 15 bit of numeric information and this leads to having 2 bit signs and 30 bits of information.
If this is true then I cannot have one Int32
into two Int16
. Is this true?
Thanks in advance.
EXTRA INFORMATION: Using Vb.Net but I think that I can translate without problems a C# answer.
What initially I wanted to do was to convert one UInt32
to two UInt16
as this is for a library that interacts with WORD based machines. Then I realized that Uint
is not CLS compliant and tried to do the same with Int32
and Int16
.
EVEN WORSE: Doing a = CType(c And &HFFFF, Int16);
throws OverflowException
. I expected that statement being the same as a = (Int16)(c & 0xffff);
(which does not throw an exception).
Jon's answer, translated into Visual Basic, and without overflow:
Results:
In .NET
CType(&Hffff, Int16)
causes overflow, and(short)0xffff
gives -1 (without overflow). It is because by default C# compiler uses unchecked operations and VB.NET checked.Personally I like Agg's answer, because my code is more complicated, and Jon's would cause an overflow exception in checked environment.
I also created another answer, based on code of
BitConverter
class, optimized for this particular task. However, it uses unsafe code.This should work:
EDIT:
tested with 0x7FFFFFFF, it works
yes it can be done using masking and bitshifts
EDIT
to answer the comment. Reconstructionworks fine:
Tested it and it prints -1 as expected.
EDIT2: changed the reconstruction. The promotion of the Int16 to Int32 caused all sign bits to extend. Forgot that, it had to be AND'ed.
Due to storage width (32bits and 16bits), converting Int32 to Int16 may imply a loss of information, if your Int32 is greater than 32767.
You can use StructLayout in VB.NET:
correction: word is 16bit, dword is 32bit
Signed would be the same just using those types instead
EDIT:
I've kind of rushed the few times I've posted/edited my anwser, and yet to explain this solution, so I feel I have not completed my answer. So I'm going to do so now:
Using the StructLayout as explicit onto a structure requires you to provide the positioning of each field (by byte offset) [StructLayoutAttribute] with the FieldOffset attribute [FieldOffsetAttribute]
With these two attributes in use you can create overlapping fields, aka unions.
The first field (DWord.Value) would be the 32bit integer, with an offset of 0 (zero). To split this 32bit integer you would have two additional fields starting again at the offset of 0 (zero) then the second field 2 more bytes off, because a 16bit (short) integer is 2 bytes a-peice.
From what I recall, usually when you split an integer they normally call the first half "high" then the second half "low"; thus naming my two other fields.
With using a structure like this, you could then create overloads for operators and type widing/narrowing, to easily exchange from say an Int32 type to this DWord structure, aswell as comparasions Operator Overloading in VB.NET
This can certainly be done with no loss of information. In both cases you end up with 32 bits of information. Whether they're used for sign bits or not is irrelevant:
Here,
reconstituted
will always equaloriginal
, hence no information is lost.Now the meaning of the signs of the two shorts is a different matter -
firstHalf
will be negative ifforiginal
is negative, butsecondHalf
will be negative if bit 15 (counting 0-31) oforiginal
is set, which isn't particularly meaningful in the original form.