473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to get two 12 bit integers from 3 bytes

Does anyone know how to recover two 12 bit integers from 3 bytes
Dec 1 '10 #1
4 10269
GaryTexmo
1,501 Expert 1GB
How much do you know about bit operations? To achieve this, you'll need to do some shifting and masking. There are 8 bits in a byte, so 3 bytes gives you 24 bits.

There are a lot of ways you can approach this, so you'll need to think about the problem, but here's the general idea...

3 bytes...
XXXXXXX YYYYYYYY ZZZZZZZZ

Map the bits from the source bytes into two 12-bit integers, represented as 16-bit (or 32-bit if you like) integers.

0000XXXX XXXXYYYY
0000YYYY ZZZZZZZZ

(Your mapping may be different, this is just the example I'm using)

You'll need two masks, one for 8 bits and another for 4 bits.

Expand|Select|Wrap|Line Numbers
  1. byte mask4bit = 0xF; // 00001111
  2. byte mask8bit = 0xFF; // 11111111
  3.  
You can get just the bits for each part using bit-wise AND (& symbol) and you can copy them into the destination using bit-wise OR (| symbol).

For the first number, you'll need to copy the first 8-bits into the destination, then you'll need to shit it left by 12 bits so you can then copy the remaining for bits in. For the second number, you'd copy the first 4 bits, shift left by 12 bits, then copy the next 8 bits over.

Does that make sense? That's the general idea, you'll need to apply it to your scenario of course. If you have any questions, please let me know.
Dec 1 '10 #2
Oralloy
985 Expert 512MB
James,

I certainly hope this isn't a coursework question. If it is, stop reading right now.

@GaryTexmo has the right of it - you'll need to use bit manipulation operators to get the job done properly.

What is important is how your input input data are structured. Without knowing this, all we can do is give you some general suggestions as to how to proceed.

For example, if the three bytes are in the bottom 24 bits of an integer type, then the extraction is very simple:
Expand|Select|Wrap|Line Numbers
  1. unsigned value1 = valueIn & 0x0fff;
  2. unsigned value2 = valueIn >> 12 & 0x0fff;
On the other hand, if you have three separate bytes that need to be combined, the logic is slightly different:
Expand|Select|Wrap|Line Numbers
  1. unsigned long valueInLong = (valueIn0<<16) | (valueIn1<<8) | (valueIn2);
  2. unsigned value1 = valueIn & 0x0fff;
  3. unsigned value2 = valueIn >> 12 & 0x0fff;
  4.  
  5. /* or */
  6.  
  7. unsigned value1 = (valueIn0<<4) | ((valueIn1&0x00f0)>>4);
  8. unsigned value2 = ((valueIn1&0x000f)<<8) | (valueIn2);
Of course, this all goes out the window, if there are big-endian versus little-endian bit ordering issues.

Cheers!
Oralloy
Dec 1 '10 #3
Thankfully not a coursework problem, I'm parsing some information from a external device talking to a port.

I have the information as byte array, its big endian and my system is little endian. As far as i can tell the format is
XXXXXXXX XXXXYYYY YYYYYYYY
Dec 2 '10 #4
Oralloy
985 Expert 512MB
Well, if you search on Yahoo for "swap bits function", you can find a fair number of ways to swap bits. The second item returned is a good forum post giving four or five different swap bits implementations.

What you do, of course, depends on your performance needs. Do you simply extract as-is, and then do a table look-up, or do you invert the bytes first, then use one of the methods suggested, above.

Luck!
Oralloy
Dec 2 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
5
by: Mantorok Redgormor | last post by:
Is it possible to display the underlying representation of integers? Not just integers but also float/double and is it also possible to display the padding bits of signed integer types in standard...
19
by: becte | last post by:
I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8) like this 11111122|22223333|33444444 Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1 and the output is int...
1
by: spamacon | last post by:
Hello, I have a strange situation using .Net FW 1.1. I want to use Marshal.PtrToStructure to fill the structure below. The first 3 fields get filled correctly: ulStruct describes how big the...
49
by: David | last post by:
I need to do an array with around 15000 integers in it. I have declared it as unsigned letter; But the compiler is saying that this is too much. How can I create an array for 15000 integers? I...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
6
by: Bint | last post by:
I have an array whose elements I'm accessing, like array, array, etc. However, the data is meant to be 16-bit words, not bytes. I'm getting byte values right now. Is there any way I can tell php...
1
by: freeurmind | last post by:
hello, i'm writing a function that translate Bytes of type unsigned char to integer array of 1's and 0's, i'm taking a message of 33 Bytes and translating them in an int array of 264 integers, here's...
4
by: parag_paul | last post by:
hi All I understand the need for long long , but what is the purpose of long as a data type separately. Just makes the language intimidating to start with, when you have to deal with so many data...
9
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I've heard of things called "trap values". From what I understand, if you set an integer variable to a trap value, then the program crashes. Is this right? From what I understand, it seems that...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.