C# bitwise rotate left and rotate right

后端 未结 6 1932
不思量自难忘°
不思量自难忘° 2020-12-09 01:42

What is the C# equivalent (.NET 2.0) of _rotl and _rotr from C++?

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 01:58

    // if you are using string
    
    string str=Convert.ToString(number,2);
    
    str=str.PadLeft(32,'0');
    
    
    
    
    //Rotate right
    
    
    str = str.PadLeft(33, str[str.Length - 1]);
    
    str= str.Remove(str.Length - 1);
    
    number=Convert.ToInt32(str,2);
    
    
    
    //Rotate left
    
    
    str = str.PadRight(33, str[0]);
    
    str= str.Remove(0,1);
    
    number=Convert.ToInt32(str,2);
    

提交回复
热议问题