Is there a nice way to split an int into two shorts (.NET)?

前端 未结 13 1669
情深已故
情深已故 2021-01-01 11:25

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

相关标签:
13条回答
  • 2021-01-01 11:55

    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:

    int original = ...;
    
    short firstHalf = (short) (original >> 16);
    short secondHalf = (short) (original & 0xffff);
    
    int reconstituted = (firstHalf << 16) | (secondHalf & 0xffff);
    

    Here, reconstituted will always equal original, hence no information is lost.

    Now the meaning of the signs of the two shorts is a different matter - firstHalf will be negative iff original is negative, but secondHalf will be negative if bit 15 (counting 0-31) of original is set, which isn't particularly meaningful in the original form.

    0 讨论(0)
  • 2021-01-01 11:55

    yes it can be done using masking and bitshifts

     Int16 a,b;
     Int32 c;
    
     a = (Int16) (c&0xffff);
     b = (Int16) ((c>>16)&0xffff);
    

    EDIT

    to answer the comment. Reconstructionworks fine:

     Int16 a, b;
     Int32 c = -1;
    
     a = (Int16)(c & 0xffff);
     b = (Int16)((c >> 16) & 0xffff);
    
     Int32 reconst = (((Int32)a)&0xffff) | ((Int32)b << 16);
    
     Console.WriteLine("reconst = " + reconst);
    

    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.

    0 讨论(0)
  • 2021-01-01 11:57

    Jon's answer, translated into Visual Basic, and without overflow:

    Module Module1
        Function MakeSigned(ByVal x As UInt16) As Int16
            Dim juniorBits As Int16 = CType(x And &H7FFF, Int16)
            If x > Int16.MaxValue Then
                Return juniorBits + Int16.MinValue
            End If
            Return juniorBits
        End Function
    
        Sub Main()
            Dim original As Int32 = &H7FFFFFFF    
            Dim firstHalfUnsigned As UInt16 = CType(original >> 16, UInt16)
            Dim secondHalfUnsigned As UInt16 = CType(original And &HFFFF, UInt16)
            Dim firstHalfSigned As Int16 = MakeSigned(firstHalfUnsigned)
            Dim secondHalfSigned As Int16 = MakeSigned(secondHalfUnsigned)
    
            Console.WriteLine(firstHalfUnsigned)
            Console.WriteLine(secondHalfUnsigned)
            Console.WriteLine(firstHalfSigned)
            Console.WriteLine(secondHalfSigned)
        End Sub
    End Module
    

    Results:

    32767
    65535
    32767
    -1
    

    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.

    0 讨论(0)
  • 2021-01-01 11:58

    I did not use bitwise operators but for unsigned values, this may work:

    public (ushort, ushort) SplitToUnsignedShorts(uint value)
    {
        ushort v1 = (ushort) (value / 0x10000);
        ushort v2 = (ushort) (value % 0x10000);
    
        return (v1, v2);
    }
    

    Or an expression body version of it:

    public (ushort, ushort) SplitToUShorts(uint value)
        => ((ushort)(value / 0x10000), (ushort)(value % 0x10000));
    

    As for signs, you have to decide how you want to split the data. There can only be 1 negative output out of two. Remember a signed value always sacrifices one bit to store the negative state of the number. And that essentially 'halves' the maximum value you can have in that variable. This is also why uint can store twice as much as a signed int.

    As for encoding it to your target format, you can either choose make the second number an unsigned short, to preserve the numerical value, or you can manually encode it such that the one bit now represent the sign of that value. This way although you will lose the originally intended numeric value for a sign bit, you don't lose the original binary data and you can always reconstruct it to the original value.

    In the end it comes down to how you want to store and process that data. You don't lose the bits, and by extension, the data, as long as you know how to extract the data from (or merge to) your encoded values.

    0 讨论(0)
  • 2021-01-01 11:59

    You can use StructLayout in VB.NET:

    correction: word is 16bit, dword is 32bit

    <StructLayout(LayoutKind.Explicit, Size:=4)> _
       Public Structure UDWord
          <FieldOffset(0)> Public Value As UInt32
          <FieldOffset(0)> Public High As UInt16
          <FieldOffset(2)> Public Low As UInt16
    
          Public Sub New(ByVal value As UInt32)
             Me.Value = value
          End Sub
    
          Public Sub New(ByVal high as UInt16, ByVal low as UInt16)
             Me.High = high
             Me.Low = low
          End Sub
       End Structure
    

    Signed would be the same just using those types instead

    <StructLayout(LayoutKind.Explicit, Size:=4)> _
       Public Structure DWord
          <FieldOffset(0)> Public Value As Int32
          <FieldOffset(0)> Public High As Int16
          <FieldOffset(2)> Public Low As Int16
    
          Public Sub New(ByVal value As Int32)
             Me.Value = value
          End Sub
    
          Public Sub New(ByVal high as Int16, ByVal low as Int16)
             Me.High = high
             Me.Low = low
          End Sub
       End Structure
    

    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

    0 讨论(0)
  • 2021-01-01 12:00

    This should work:

    int original = ...;
    byte[] bytes = BitConverter.GetBytes(original);
    short firstHalf = BitConverter.ToInt16(bytes, 0);
    short secondHalf = BitConverter.ToInt16(bytes, 2);
    

    EDIT:

    tested with 0x7FFFFFFF, it works

    byte[] recbytes = new byte[4];
    recbytes[0] = BitConverter.GetBytes(firstHalf)[0];
    recbytes[1] = BitConverter.GetBytes(firstHalf)[1];
    recbytes[2] = BitConverter.GetBytes(secondHalf)[0];
    recbytes[3] = BitConverter.GetBytes(secondHalf)[1];
    int reconstituted = BitConverter.ToInt32(recbytes, 0);
    
    0 讨论(0)
提交回复
热议问题