Anonymous
Anonymous

Reputation: 557

Insert 2 numbers into a byte

I need to insert 2 bits of data into a single byte.

First 3 bits (0,1,2) to contain a number between 1 and 5.

Last 5 bits (3,4,5,6,7) to contain a number between 0 and 25. [Edit: Changed from 250]

I tried:

byte mybite = (byte)(val1 & val2)

but to be honest I don't really know what I'm doing with bit operations although I had some help in reading this info from an earlier post which was great.

This is how I read the info from a byte:

 // Advanced the position of the byte by 3 bits and read the next 5 bits
 ushort Value1 = Convert.ToUInt16((xxx >> 3) & 0x1F);

 // Read the first 3 bits
 ushort Value2 = Convert.ToUInt16((xxx & 0x7));

Thanks in advance.

Upvotes: 4

Views: 499

Answers (4)

user1429080
user1429080

Reputation: 9166

If I understand you question correctly, you are looking for a reverse operation to the one you had in you question.

Here is how you can do that (ugly code with casts etc, but shows what's happening with the bits):

byte source = 0xA3; // source = 10100011 = 163
// get bits from source
byte lowbits = (byte)((int)source & 7); // lowbits = 00000011 = 3
byte highbits = (byte)((int)source >> 3); // highbits = 00010100 = 20

Note though that at this point, lowbits contains a value that is between 0 and 7 (not 0 and 5), while highbits contains a value that is between 0 and 31 (not 0 and 250).

// use bits to create copy of original source
byte destination = (byte)(((int)highbits << 3) | (int)lowbits); // destination = 10100011

Also note that if highbits contains a value greater than 31, then some bits will be dropped by this operation. And if lowbits contain a value greater than 7, it may cause some bits from highbits to be overwritten.

Upvotes: 0

altschuler
altschuler

Reputation: 3922

int num1 = 4;
int num2 = 156;
int num = (num2 << 3) | num1;

Then you can read num2 by shifting 3 to the right

int num2Read = num >> 3

And you read num1 like (you are creating something like a mask that &'s the first 3 bits of num)

int num1Read = num & 7

This way, the first number can be 3 bits and the second number can be arbitrarily long

Upvotes: 6

Royi Namir
Royi Namir

Reputation: 148524

(If I understand your question , you want to add bits to a certain location) Byte is xxxx-xxxx

so if you want to "add" to the right most bit : xxxx-xxxY

byte b=...

b=b | 1

If you want to add to the second most right bit : xxxx-xxYx

b=b | 2

If you want to add to the third most right bit : xxxx-xYxx

b=b | 4

If you want to add to the fourth most right bit : xxxx-Yxxx

b=b | 8

If you want to add to the fifth most right bit : xxxY-xxxx

b=b | 16

differnet example :

if you want to add 14 :

just do

b=b | 14 which will upbit the xxxx-YYYx bits

Upvotes: 1

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

Use or instead of and operation. IE

byte mybite = (byte)(val1 | val2);

If val1 is 0000-0010 (2) and val2 is 1000-0000 (128), and (&) will result in 0000-0000, while or (|) will result in 1000-0010 (130).

Upvotes: 0

Related Questions