萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

C# 與 VB.NET 位元運算增強庫:移位、循環移位、二進制字串轉換完整實作

痛點:高階語言位元運算支援不足

C#/VB.NET 缺乏 C/C++ 風格的循環移位、任意寬度位元操作,且 <</>> 僅支援 32/64 位整數。封裝通用位元操作類庫,統一介面、擴展功能。

類庫架構與核心 API

Namespace BitOperatorLibrary
    Namespace ShiftOperation
        Module NormalShiftOperation     ' 邏輯/算術移位
            Function _shl_8/16/32/64_(value, bits) As Integer
            Function _shr_8/16/32/64_(value, bits) As Integer
        End Module
        Module CircularShiftOperation   ' 循環移位 (Rotate)
            Function _rol_8/16/32/64_(value, bits) As Integer
            Function _ror_8/16/32/64_(value, bits) As Integer
            Function _crol_8/16/32/64_(value, bits) As Integer ' 帶進位循環
            Function _cror_8/16/32/64_(value, bits) As Integer
        End Module
    End Namespace
    Module bitConvert                   ' 二進制字串互轉
        Function GetBinaryString(value, Optional bits) As String
        Function SetBitState(value, pos, state) As Long
        Function GetBitState(value, pos) As Boolean
    End Module
End Namespace

VB.NET 使用範例

Imports BitOperatorLibrary.ShiftOperation.CircularShiftOperation
Imports BitOperatorLibrary.ShiftOperation.NormalShiftOperation
Imports BitOperatorLibrary.bitConvert

Module Module1
    Sub Main()
        Console.WriteLine("0x2F2E 左移1位: 0x{0:X}", _shl_16_(&H2F2E, 1))
        Console.WriteLine("0x2F2E 循環左移1位: 0x{0:X}", _crol_16_(&H2F2E, 1))
        Console.WriteLine("0x2F2E 右移1位: 0x{0:X}", _shr_16_(&H2F2E, 1))
        Console.WriteLine("0x2F2E 循環右移1位: 0x{0:X}", _cror_16_(&H2F2E, 1))
        Console.WriteLine("0xFE1A2D 二進制: {0}", GetBinaryString(&HFE1A2D))
        Console.WriteLine("0xFE1A2D 設第8位為1: 0x{0:X}", SetBitState(&HFE1A2D, 7, True))
        Console.ReadKey()
    End Sub
End Module

C# 等效用法

using BitOperatorLibrary;
ushort val = 0x2F2E;
Console.WriteLine($"左移: 0x{NormalShift._shl_16_(val, 1):X}");
Console.WriteLine($"循環左移: 0x{CircularShift._crol_16_(val, 1):X}");
Console.WriteLine($"二進制: {BitConvert.GetBinaryString(0xFE1A2D)}");
Console.WriteLine($"設位: 0x{BitConvert.SetBitState(0xFE1A2D, 7, true):X}");

核心演算法實作細節

循環左移 (ROL)

public static ushort _rol_16_(ushort value, int bits) {
    bits %= 16;
    return (ushort)((value << bits) | (value >> (16 - bits)));
}

帶進位循環左移 (CROL)

public static ushort _crol_16_(ushort value, int bits) {
    bits %= 16;
    bool carry = (value & 0x8000) != 0;
    ushort res = (ushort)((value << bits) | (value >> (16 - bits)));
    if (carry) res |= 1; // 將原最高位放入最低位
    return res;
}

二進制字串轉換

public static string GetBinaryString(long value, int bits = 32) {
    var sb = new StringBuilder(bits);
    for (int i = bits - 1; i >= 0; i--)
        sb.Append((value >> i) & 1);
    return sb.ToString();
}

效能與正確性驗證

  • 所有方法為 Pure Function,無副作用,易於單元測試。
  • 針對 8/16/32/64 位元分別實作,避免泛型裝箱開銷。
  • 循環移位位數自動取模,防止位移量超過位寬導致未定義行為。

擴充建議

  • 加入 BitArray 互轉、位元計數、前導/尾隨零計數。
  • 提供 Span<byte> 版本支援記憶體區塊操作。
  • 發布為 NuGet 套件,支援 .NET Standard 2.0+ 跨平台。

將位元操作封裝為獨立類庫,不僅解決語言缺口,更能統一團隊編碼風格,減少手寫位運算的 Bug。

copyright © 萬盛學電腦網 all rights reserved