簡體   English   中英

從按位或組合整數中刪除值?

[英]Remove value from bitwise or-combined integer?

使用按位或運算符可以將整數(例如,2的冪的整數)相互組合,然后檢查返回的int是否包含指定的值。 但是,是否有適當的方法可以在不合並新整數的情況下從返回的整數中刪除值?

目前,我從合並的整數中減去了要刪除的值,但這可以正常工作還是會引起問題? 還是有其他“適當”的方式來做到這一點?

這是我的代碼:

private void button1_Click(object sender, EventArgs e)
{
    int combinedints = CombineIntegers(CombineIntegers(2, 8), 32); //Combine three integers.
    MessageBox.Show(combinedints.ToString()); //Shows 42.
    MessageBox.Show(RemoveInteger(combinedints, 8).ToString()); //Removes 8 - shows 34.
}

private int CombineIntegers(int a, int b)
{
    return a | b;
}

private int RemoveInteger(int a, int b)
{
    return a -= b;
}

private bool CheckInteger(int a, int b)
{
    return (a & b) == b;
}
private int RemoveInteger(int a, int b)
{
    return a & ~b;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM