Hello,
I have some data that stores two 16-bit integers in a 32-bit float, and
I would like to extract the data. I want to do something like
//-----------------------------------------------------------------
float *data;
short *buff;
int dataLen; //number of points in data
//other code...
for (i = 0; i < dataLen; i++)
{
buff[2*i] = (data[i] & 0x0F); // place the lower 16 bits in the
first sample
buff[2*i + 1] = (data[i] & 0xF0) >> 16; //place the upper 16 bits
(and bit shifted)in the second sample
}
//------------------------------------------------------------------
Does this make sense? I'm thinking it will involve casting the data
variable as an int/short somehow, using something like:
buff[2*i] = ( *(int*)&data[i] ...); //found this on another forum, does
this work?
Any ideas?
Thanks! 6 8398 ja*******@gmail .com wrote: I have some data that stores two 16-bit integers in a 32-bit float,
WHY??? Can't you store those integers in a 32-bit _integer_? Use
'long' or 'unsigned long'.
and I would like to extract the data. I want to do something like
//----------------------------------------------------------------- float *data; short *buff; int dataLen; //number of points in data
//other code...
for (i = 0; i < dataLen; i++) { buff[2*i] = (data[i] & 0x0F); // place the lower 16 bits in the first sample
That's not 16 bits. That's 4 bits. 0x0F == 0b00001111.
buff[2*i + 1] = (data[i] & 0xF0) >> 16; //place the upper 16 bits (and bit shifted)in the second sample
That's not "upper 16 bits". That's 0.
} //------------------------------------------------------------------
Does this make sense?
No.
I'm thinking it will involve casting the data variable as an int/short somehow, using something like:
buff[2*i] = ( *(int*)&data[i] ...); //found this on another forum, does this work?
Any ideas?
Don't do it.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
>WHY??? Can't you store those integers in a 32-bit _integer_? Use 'long' or 'unsigned long'.
Ok, let me clarify. I am reading data from hardware that compresses two
16-bit integer values into a single 32-bit float. I do not have any
control over this; the data is read into my program as a float.
That's not 16 bits. That's 4 bits. 0x0F == 0b00001111.
Oops. It is correct in my program (i.e. 0x0000FFFF).
So, given that the data must be stored in a float, and that the float
contains 2 16-bit integers, how do I get to the integers? ja*******@gmail .com wrote: WHY??? Can't you store those integers in a 32-bit _integer_? Use 'long' or 'unsigned long'.
Ok, let me clarify. I am reading data from hardware that compresses two 16-bit integer values into a single 32-bit float. I do not have any control over this; the data is read into my program as a float.
OK... Really? Wow. Floats, eh? Well, fine. That's not 16 bits. That's 4 bits. 0x0F == 0b00001111.
Oops. It is correct in my program (i.e. 0x0000FFFF).
So, given that the data must be stored in a float, and that the float contains 2 16-bit integers, how do I get to the integers?
Non-portable way would be to extract chars (unsigned are probably better)
from the same address as your floats and reconstruct the integers:
void float_to_2_ints (float f, int i[2])
{
char* pc = static_cast<cha r*>(&f);
i[0] = (((unsigned char)pc[0]) << 8) | ((unsigned char)pc[1]);
i[1] = (((unsigned char)pc[2]) << 8) | ((unsigned char)pc[3]);
}
If the order of integers and/or chars in the float is different than the
one assumed here, you'd need to swap "pc[0/2]" with "pc[1/3]" and/or "i[0]"
with "i[1]". It all depends on the endianness of the hardware and your
system.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
> Ok, let me clarify. I am reading data from hardware that compresses two 16-bit integer values into a single 32-bit float. I do not have any control over this; the data is read into my program as a float.
That's not 16 bits. That's 4 bits. 0x0F == 0b00001111.
Oops. It is correct in my program (i.e. 0x0000FFFF).
So, given that the data must be stored in a float, and that the float contains 2 16-bit integers, how do I get to the integers?
At first, the question seems to be a topic of C programming not C++.
bitwise and shift perhaps depend on the compression format. I think you
need to clarify:
(1) two 16bit intergers only occupy the space of 32-bit float , or
(2) two 16bit integers stored in the float format (some hardwares
support, see your hardware manual)
In the first case, bitwise and >> are not correct in your code.
e.g. buff[2*i] = (data[i] & 0x0000FFFF)
you cannot bitwise between float and short.
one simple way is to move or copy the memory content between them
directly.
for (i = 0; i < dataLen; i++)
{
/* data[i] 32bit -> buff[2*i], buff[2*i+1] */
memmov( (char *) &(buff[2*i]), (char *) &data[i], sizeof(short) * 2)
.....
}
If your case is the second one, read your hardware manual by yourself.
On Tue, 18 Apr 2006 12:44:25 -0400 "Victor Bazarov"
<v.********@com Acast.net> waved a wand and this message magically
appeared: Ok, let me clarify. I am reading data from hardware that compresses two 16-bit integer values into a single 32-bit float. I do not have any control over this; the data is read into my program as a float.
OK... Really? Wow. Floats, eh? Well, fine.
I *really* would like to know what sort of braindamaged hardware
does this and who the hardware designers were so I can shoot them.
-- http://www.munted.org.uk
Take a nap, it saves lives.
Alex Buell wrote: On Tue, 18 Apr 2006 12:44:25 -0400 "Victor Bazarov" <v.********@com Acast.net> waved a wand and this message magically appeared:
Ok, let me clarify. I am reading data from hardware that compresses two 16-bit integer values into a single 32-bit float. I do not have any control over this; the data is read into my program as a float.
OK... Really? Wow. Floats, eh? Well, fine.
I *really* would like to know what sort of braindamaged hardware does this and who the hardware designers were so I can shoot them.
I bet it's not hardware designers. Hardware just sends integers, two in
a row, probably. It's software designers who develop the interface to
expose them as 'float' types. Which actually kindof makes me think, what
if you instead of declaring the 'float*' on the receiving side, declare
'long*'? It's so much easier to unpack. And the sizes are most likely
the same... If you don't care about portability, cheat _them_ and right
there, where it all begins, instead of cheating yourself later.
V
--
Please remove capital As from my address when replying by mail This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Francesco Moi |
last post by:
Hello.
I designed a form to edit some DataBase's fields. But some
of these fields contain '<' and '>' characters. And
these characters are '<' and '>' in HTML.
So if want to edit these fields, the form converts them
into '<' and '>'. And I want to mantain '<' and '>'
Mi piece of code:
|
by: Fabrício de Novaes Kucinskis |
last post by:
Hi all,
I'm doing some bitwise operations in VB.NET, and I got some strange results.
If an Int16 (Short) variable has the value &HAA (8 bits) and I do a 4-bit
shift:
var16 = var16 << 4
I got, correctly, the value &HAA0. But, if I try to do a 8-bit shift, I got
|
by: Joe Gonzalez |
last post by:
Is it possible to perform bitwise operations in selects in DB2 8.1? In
postgres I can say something like SELECT ... FROM <sometable> WHERE
RecFlags && 0x08 <> 0 to get a bitwise and operation. I can't find
anything similar in the db2 SELECT docs. Is there a built-in function
that can perform the bitwise operation? Thanks in advance for any...
|
by: Christopher Weaver |
last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the
bitwise AND of 8 and 9 will return 1 or true but I don't know how to write
the synax for it in C#. I have a value that ranges from 0 to 15 and I need
to compare it to 15 in order to find if it contains the values 1, 2, 4, or
8.
To represent it more graphically, the value...
|
by: Emilio |
last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
| |
by: AMDRIT |
last post by:
Gang,
I always get confused when it comes to 1's and 0's. I would like to perform
a bitwise operation on a value based on checked boxes.
Am I doing this right?
assuming TurmsRemote.Weekdays has values of
|
by: alex |
last post by:
hi friends ...
i am facing a problem while detecting floating point operations in my
project, please help me.
i want to find out the places in my C/C++ project where i am doing
floating point operations.
As it is a big project it is not possible to check every line manually,
so is there any other method
to detect floating point...
|
by: Rob Wilkerson |
last post by:
I'm attempting to do some work around existing code that uses bitwise
operations to manage flags passed into a function and I'm quite
frankly unequipped to do so. I've never done much with bitwise
operations and all the reading I've done today doesn't appear to be
helping me much. I'm hoping someone here can provide a remedial
lesson. ...
|
by: Daniel Gutson |
last post by:
Hi,
I just wanted to share another library for doing type-safe bitwise
operations in C++:
http://bitwise-enum.googlecode.com
I found it useful, so hopefully it'll be for somebody else as well.
BRgds,
Daniel.
|
by: bretonlais |
last post by:
I am creating a scheduling application which will allow users to
schedule recurring tasks, e.g., the user can set a task to run (M,T,W)
or (M,T,W,TH,F,S) etc. I want binary encode each day of the week and
use a bitwise OR and AND to determine which days the user selected
(and all the permutations):
const None = 0x0;
const Sunday = 0x1;...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |