使用掩码在C#中进行位操作 [英] Bit manipulation in C# using a mask
本文介绍了使用掩码在C#中进行位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些有关C#中位图操作的帮助
I need a little help with bitmap operations in C#
我想取一个UInt16
,隔离任意数量的位,然后使用另一个UInt16
值进行设置.
I want to take a UInt16
, isolate an arbitrary number of bits, and set them using another UInt16
value.
示例:
10101010 -- Original Value
00001100 -- Mask - Isolates bits 2 and 3
Input Output
00000000 -- 10100010
00000100 -- 10100110
00001000 -- 10101010
00001100 -- 10101110
^^
推荐答案
似乎想要:
(orig & ~mask) | (input & mask)
前半部分将mask
中的orig
位清零.然后,对mask
中的input
中的位进行按位或运算.
The first half zeroes the bits of orig
which are in mask
. Then you do a bitwise OR against the bits from input
that are in mask
.
这篇关于使用掩码在C#中进行位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文