Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 bytes represent 3 integers in C

Tags:

c

I have two bytes, 8 bit octets, which should be read as: [3 bits][4 bits][3 bits].

Example:

unsigned char octet1 = 0b11111111;  // binary values
unsigned char octet2 = 0b00000011;

As integers: [7][15][7].

Anybody can give me a hint where to start?

like image 597
Doori Bar Avatar asked Nov 04 '10 11:11

Doori Bar


People also ask

What is a 2-byte integer?

2-byte signed Integer [the ~] noun – An automation integer data type that can be either positive or negative. The most significant bit is the sign bit, which is 1 for negative values and 0 for positive values. The storage size of the integer is 2 bytes. A 2-byte signed integer can have a range from -32,768 to 32,767.

How many characters is 2 bytes?

a character in binary is a series of 8 on or offs or 0 or 1s. one of those is a bit and 8 bits make a byte so 1 byte is one character.so 2 bytes hold two characters.

How do you represent 2 bytes?

2 bytes would be 4 decimal digits and you could represent values between 0 up to 10000 (not included).


1 Answers

In a kind of pseudocode

octet1 = 0b11111111
octet2 = 0b00000011
word = octet1 | octet2<<8
n1 = word & 0b111
n2 = word>>3 & 0b1111
n3 = word>>7 & 0b111
like image 75
Jonas Elfström Avatar answered Oct 20 '22 20:10

Jonas Elfström