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

前端 未结 13 1667
情深已故
情深已故 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 12:04

    Int32 num = 70000;

            string str = Convert.ToString(num, 2);
        //convert INT32 to Binary string   
            Int32 strl = str.Length;
        //detect string length
    
    
            string strhi, strlo;
        //ifvalue is greater than 16 bit 
            if (strl > 16)
            {
               int lg = strl - 16;
              //dtect bits in higher word 
               strlo = str.Substring(lg, 16);
         ///move lower word string to strlo 
    
                strhi = str.Substring(0, lg);
       //mov higher word string to strhi
    
            }
            else
    //if value is less than 16 bit
            {
               strhi = "0";
    //set higher word zero
               strlo = str;
    ///move lower word string to strlo 
    
            }
    
            Int16 lowword, hiword;
            lowword = Convert.ToInt16(strlo, 2);
            hiword = Convert.ToInt16(strhi, 2);
            ////convert binary string to int16
            }
    

提交回复
热议问题