## Bit manipulations

Hi,

Today, I will explain bit manipulations. As you know each bit set in its own position from 0 to 7.

As an example, Lets use the following byte 00000010 which is 0x02 in hex-decimal and 2 in decimal.

It has "1" at 1st place (lets count it from zero place to 7 place).

So shifting bits to the left will move all bits to the left:

00000010 << 2 = 00001000

As you see now 1 on the 3rd place, instead of 1st place, because of we have shifted all bits to the left 2 times. Same rule is for the right shifting.

Another type of bit manipulation is boolean operations like XOR, OR and AND.

OR just sets 1 on its place:

#### 0010 OR 0001 = 0011

0010 OR 0010 = 0010

XOR makes the same as OR and inverts bits at the same places if they both equal to 1 (outputs 1 only when both bits differ):

#### 0010 XOR 0001 = 0011

0010 XOR 0010 = 0000

#### AND leaves bits that equal to 1 at the same place only:

Rectangle rect = new Rectangle(0, 0, tmpi.Width, tmpi.Height);

BitmapData bmpd = tmpi.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

Int32[] src = new Int32[bmpd.Stride / 4 * tmpi.Height];

System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, src, 0, src.Length);

public void SetPointAsIs(byte r, byte g, byte b, byte a, int x, int y)

{

src[Stride * Y + x] = (a << 24) + (r << 16) + (g << 8) + b;

}

public ColorBgra GetPoint(int x, int y)

{

Int32 argb = src[Stride * y + x];

ColorBgra c = new ColorBgra();

c.A = (byte)(argb >> 24);

c.R = (byte)(argb >> 16 & 0xff);

c.G = (byte)(argb >> 8 & 0xff);

c.B = (byte)(argb & 0xff);

return c;

}

## Add comment

Cancel reply to comment