# Working with A selection of bits

• ### Question

• Hi

I hope someone can help

I have the Bytes 9F03

1001111100000011

I need to extract bits 0-13 as a seperate value and bits 14-15 as a value.

bits 0-13 need to be a Int value I can use for calculations.

bits 14-15 a truth table.

00 =

01 =

10 =

11 =

Monday, May 13, 2019 9:40 AM

### All replies

• You can use the bit-wise operators in C#:

```int theNumber = 0x9F03;

int value0to14 = theNumber & 0x7fff;

int bits15to16 = (theNumber >> 15) & 0x03;

switch (bits15to16)
{
case 0: ...; break;
case 1: ...; break;
case 2: ...; break;
case 2: ...; break;
}```

Note that here I am presuming that you count the bits from the least significant, i.e., bits 0 to 14 would be the 15 least significant bits. Also note that based on your mention of "0 to 14" this means that you are counting your bits from zero, so if you are using bits 15 and 16 this means that you presume that your number has 17 bits (not 16). But your example of 9F03 only has 16.

Monday, May 13, 2019 12:17 PM
• This does not work.

The first 14 bits from right to left is a value in this case 7939. <- Decimal

regardless of what the last 2 bits are. in this case 10 <- Binary

I need to capture both parts and evaluate each part seperately.

For instance if the Byte was 0x4859 then,

first 14 bits = 2137 <- Decimal

and the last 2  = 01 <- Binary.

Monday, May 13, 2019 1:01 PM
• first 14 bits = 2137 <- Decimal

You have not read properly my reply. Note how I insisted that your specification for "bits 0 to 14" was not the same as "the first 14 bits". From 0 to 14 there are fifteen bits. Of course the results do not match if you want 14 bits instead of fifteen, and if your "last two bits" are bits 14 and 15 (counting from zero) instead of 15 and 16 as you specified in the first place.

Just go back to my response and change the numbers to reflect the bits that you really want instead of the bits that you specified wrongly in the first place.

```int theNumber = ...;

int first14bits = theNumber & 0x3fff; // the first 14 bits are bits 0 to 13 (not 0 to 14)

int topmost2bits = (theNumber >> 14) & 0x03;```

Monday, May 13, 2019 1:08 PM
• And yet this still doesn't work.

Monday, May 13, 2019 1:13 PM
• Sorry, should be 3fff instead of 4fff.

I've gone back to the previous response and fixed the value there.

The initial 7fff was correct for 15 bits, but when I adjusted for 14 bits I made a mistake.

Monday, May 13, 2019 1:27 PM
• Hi BinaryWiggles,

Thank you for posting here.

Based on your description, you want to extract the bytes to two parts.

You could try the following code.

```            int theNumber = 0x9F03;
string m = Convert.ToString(theNumber, 2);
string s = m.Substring(0, 14);
string p = m.Substring(14, 2);
int first14 = Convert.ToInt32(s, 2);
Console.WriteLine("0~13 value is {0} 14~15 values is {1}",first14,p);```

Result:

Best Regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Tuesday, May 14, 2019 3:09 AM

• Based on your description, you want to extract the bytes to two parts.

You could try the following code.

```            int theNumber = 0x9F03;
string m = Convert.ToString(theNumber, 2);
string s = m.Substring(0, 14);
string p = m.Substring(14, 2);
int first14 = Convert.ToInt32(s, 2);
Console.WriteLine("0~13 value is {0} 14~15 values is {1}",first14,p);```

Result:

Jack -

There are some issues with the code you posted that will make it yield different
results than the code posted by Alberto. First note that when you use

string m = Convert.ToString(theNumber, 2);

leftmost zeros will be omitted in the string. To maintain 16 bits so that
subsequent SubString operations will not fail you will need to pad the string
on the left with zeros:

```string m = Convert.ToString(theNumber, 2).PadLeft(16, '0');
```

Secondly, you appear to be taking as the topmost 2 bits those from the opposite
end of the binary string here:

```string s = m.Substring(0, 14);
string p = m.Substring(14, 2);

//To get results that are consistent with what Alberto's code produces,
//you need to change these lines to:

string s = m.Substring(2, 14);
string p = m.Substring(0, 2);

```

Then for the value 0x4859 your code will show:

0~13 value is 2137 14~15 values is 01

Alberto's code will show

first14bits == 2137

topmost2bits == 1

- Wayne

Tuesday, May 14, 2019 5:19 AM
• Hi

I hope someone can help

I have the Bytes 9F03

1001111100000011

I need to extract bits 0-13 as a seperate value and bits 14-15 as a value.

bits 0-13 need to be a Int value I can use for calculations.

bits 14-15 a truth table.

00 =

01 =

10 =

11 =

There are some ambiguities in your question:

1. What is the .Net data type of the input (e.g. the "bytes" 9F03)

2. What is the desired data type of the bits 0-13?

3. Is there a sign bit in bits 0-13, I mean do you want the result signed or unsigned?

Also how about some expected values, in your example what would you expect to see for the 14 bit integer value and what would you expect for the truth table bit pairs, give like two or three examples.

Saturday, June 1, 2019 1:20 PM
• Hi

I hope someone can help

I have the Bytes 9F03

1001111100000011

I need to extract bits 0-13 as a seperate value and bits 14-15 as a value.

bits 0-13 need to be a Int value I can use for calculations.

bits 14-15 a truth table.

00 =

01 =

10 =

11 =

There are some ambiguities in your question:

1. What is the .Net data type of the input (e.g. the "bytes" 9F03)

2. What is the desired data type of the bits 0-13?

3. Is there a sign bit in bits 0-13, I mean do you want the result signed or unsigned?

Also how about some expected values, in your example what would you expect to see for the 14 bit integer value and what would you expect for the truth table bit pairs, give like two or three examples.

Having said that, does this work?

```using System;

namespace ConsoleApp12
{
class Program
{
static void Main(string[] args)
{
Byte[] bytes = new byte[2];

bytes[1] = 0x9F;
bytes[0] = 0x03;

var breakdown = new Breakdown(bytes);
}
}

public sealed class Breakdown
{
public Breakdown(Byte[] Input)
{
if (Input == null) throw new ArgumentNullException(nameof(Input));
if (Input.Length != 2) throw new ArgumentOutOfRangeException(nameof(Input), "The input must contain exactly two elements.");

var binary = BitConverter.ToUInt16(Input, 0);
Value = (Int16)(0x3FFF & binary);
TruthTable = (Table)((0xC000 & binary) >> 14);
}

public Int16 Value { get; private set; }
public Table TruthTable { get; private set; }
}

public enum Table : uint
{
ZERO,
ONE,
TWO,
THREE
}
}
```

Saturday, June 1, 2019 1:43 PM