比特的一点点

扫码查看
本文介绍了比特的一点点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要将一些数据类型写入一个无符号字符数组中。


这些基本上是信号。在一条消息中,所以每个信号都有一个起始位和一个长度。信号也有一个类型,其中一个:

unsigned long

签名长

浮动

double


float和double的信号长度为32或64位,因为

合适。


我需要只是移动位并将它们扔进阵列中,不受影响。

最好的方法是什么?


因为我不能移位一个浮动,我想到了

4类型的联合,并且使用无符号类型做了所有的苦恼。

这会起作用,还是我在找麻烦?大概有一个更好的方式。


欢呼任何见解,


Ben

-

我不仅仅是一个数字。对很多人来说,我被称为弦乐......

Hi,

I need to write some data types into an array of unsigned chars.

These are basically "signals" within a message, so each signal will have
a start bit and a length. The signals will also have a type, one of:
unsigned long
signed long
float
double

The signals with float and double will be 32 or 64 bits long, as
appropriate.

I need to just shift the bits and throw them into the array, untouched.
Whats the best way of doing this?

Since I can''t bit shift a float around, I was thinking of a union of the
4 types, and doing all the bit twiddling using the unsigned type. Is
this going to work, or am I asking for trouble? Presumably there''s a
better way.

Cheers for any insights,

Ben
--
I''m not just a number. To many, I''m known as a String...

推荐答案




我不确定我完全理解你想要完成的事情。 />
但看起来像std :: bitset可以帮到你。它是一个位向量和

提供对位的恒定时间访问。您也可以转换

位并使用按位运算符。


Srini



I''m not sure I completely understand what you''re trying to accomplish.
But looks like std::bitset can help you out here. Its a bit-vector and
provides constant time access to the bits. You can also shift around
bits and use bitwise operators on it.

Srini





好​​的,我正在尝试将信号打包到数据帧中,以便通过

通信频道发送。这些信号可以有4种不同的类型,每个帧的先验已知

。信号有一个长度(可能会受到它的类型限制),以及帧内的起始位。


所以,一个信号类型为float,有32位,并且可以从64位数据帧中的5位开始说。

但看起来像std :: bitset可以帮到你在这里。它的位向量和
提供对位的恒定时间访问。你也可以转移
位并在其上使用按位运算符。



OK, I''m trying to pack signals into a data frame to be sent over a
communications channel. Those signals can have 4 distinct types, which
are known a priori for each frame. The signals have a length (which may
be restricted by it''s type), and a start bit within the frame.

So, a Signal of type float, would have 32 bits, and could start at say,
the 5 bit within a 64bit data frame.
But looks like std::bitset can help you out here. Its a bit-vector and
provides constant time access to the bits. You can also shift around
bits and use bitwise operators on it.




这很有意思,但推荐的方法是什么?

在我的浮动进出bitset?


Ben

-

我不是只是一个数字。对很多人来说,我称之为弦乐......



Thats interesting, but what is the recommended way of getting the bits
in my float into and out of the bitset?

Ben
--
I''m not just a number. To many, I''m known as a String...




这很有意思,但建议的方法是将我的浮点数放入并且不在bitset?



Thats interesting, but what is the recommended way of getting the bits
in my float into and out of the bitset?




我不知道有任何可移植的方法。如果你想将浮点变量中的位包装到帧中,你可以做一个(一个丑陋的)

reinterpret_cast。

Ben
-
我不仅仅是一个数字。对很多人来说,我称之为弦乐......



I do not know of any portable way of doing this. If you want to pack
the bits from a float variable into the frame, you could do a (an ugly)
reinterpret_cast.
Ben
--
I''m not just a number. To many, I''m known as a String...




Srini



Srini


这篇关于比特的一点点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 16:17
查看更多