字节:移位双打? [英] bytes: shifting doubles?
问题描述
嗨
我想换一个双,但我得到了
错误消息''>>''非法,左操作数翻倍。
虽然这些联机帮助页说''>>''和''<<'''可以
仅适用于int',我可以在unsigned longs上使用
移位运算符而没有任何问题。
有谁知道如何正确地做到这一点?
即单独将字节复制成长数组:
------------- -------------------
int指针;
char databuffer [100];
double uint64;
unsigned long uint32;
pointer = 0;
/ /这个工作:
uint32 = 1111111; //任何
databuffer [pointer ++] = uint32>> 24;
databuffer [pointer ++] = uint32>> 16;
databuffer [pointer ++] = uint32>> 8;
databuffer [pointer ++] = uint32>> 0; // jup,我知道..!
//这不会:
uint64 = 1111111; //任何
databuffer [pointer ++] = uint64>> 56;
databuffer [pointer ++] = uint64>> 48;
databuffer [pointer ++] = uint64>> 40;
databuffer [pointer ++] = uint64>> 32;
databuffer [pointer ++] = uint64>> 24;
databuffer [pointer ++] = uint64>> 16;
databuffer [pointer ++] = uint64>> 8;
databuffer [pointer ++] = uint64>> 0;
[见 http: //www.gotw.ca/resources/clcm.htm 了解有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
hi
i''m trying to shift a double, but i''m getting the
error message ''>>'' illegal, left operand double.
althought that the manpages say, ''>>'' and ''<<'' can
be applied for int''s only, i was able to use the
shift operator on unsigned longs without any problems.
does anyone know how to do this properly?
i.e. individually copying bytes into a long array:
--------------------------------
int pointer;
char databuffer[100];
double uint64;
unsigned long uint32;
pointer =0;
// this works:
uint32 = 1111111; // anything
databuffer[pointer++] = uint32 >> 24;
databuffer[pointer++] = uint32 >> 16;
databuffer[pointer++] = uint32 >> 8;
databuffer[pointer++] = uint32 >> 0; // jup, i know..!
// this won''t:
uint64 = 1111111; // anything
databuffer[pointer++] = uint64 >> 56;
databuffer[pointer++] = uint64 >> 48;
databuffer[pointer++] = uint64 >> 40;
databuffer[pointer++] = uint64 >> 32;
databuffer[pointer++] = uint64 >> 24;
databuffer[pointer++] = uint64 >> 16;
databuffer[pointer++] = uint64 >> 8;
databuffer[pointer++] = uint64 >> 0;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
Allan Rydberg写道:
Allan Rydberg wrote:
hi
我''我试图转移一个双,但我得到
错误消息''>>''非法,左操作数加倍。
虽然这些联机帮助页说,'' >>''和''<<'''只能应用于int',我可以在unsigned longs上使用
shift运算符而不会出现任何问题。有没有人知道如何正确地做到这一点?
即将字节单独复制到一个长数组中:
------------------ --------------
int指针;
char databuffer [100];
double uint64;
unsigned long uint32;
指针= 0;
//这个有效:
uint32 = 1111111; //任何
databuffer [pointer ++] = uint32>> 24;
databuffer [pointer ++] = uint32>> 16;
databuffer [pointer ++] = uint32>> 8;
databuffer [pointer ++] = uint32>> 0; // jup,我知道..!
//这不会:
uint64 = 1111111; //任何
databuffer [pointer ++] = uint64>> 56;
databuffer [pointer ++] = uint64>> 48;
databuffer [pointer ++] = uint64>> 40;
databuffer [pointer ++] = uint64>> 32;
databuffer [pointer ++] = uint64>> 24;
databuffer [pointer ++] = uint64>> 16;
databuffer [pointer ++] = uint64>> 8;
databuffer [pointer ++] = uint64>> 0;
hi
i''m trying to shift a double, but i''m getting the
error message ''>>'' illegal, left operand double.
althought that the manpages say, ''>>'' and ''<<'' can
be applied for int''s only, i was able to use the
shift operator on unsigned longs without any problems.
does anyone know how to do this properly?
i.e. individually copying bytes into a long array:
--------------------------------
int pointer;
char databuffer[100];
double uint64;
unsigned long uint32;
pointer =0;
// this works:
uint32 = 1111111; // anything
databuffer[pointer++] = uint32 >> 24;
databuffer[pointer++] = uint32 >> 16;
databuffer[pointer++] = uint32 >> 8;
databuffer[pointer++] = uint32 >> 0; // jup, i know..!
// this won''t:
uint64 = 1111111; // anything
databuffer[pointer++] = uint64 >> 56;
databuffer[pointer++] = uint64 >> 48;
databuffer[pointer++] = uint64 >> 40;
databuffer[pointer++] = uint64 >> 32;
databuffer[pointer++] = uint64 >> 24;
databuffer[pointer++] = uint64 >> 16;
databuffer[pointer++] = uint64 >> 8;
databuffer[pointer++] = uint64 >> 0;
unsigned long通常与unsigned int相同,因此可以正常工作。如果你想要改变64位的价值,你可以在msvc ++下使用长gcc和__int64
(?)。
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。审核。第一次海报:做到这一点! ]
unsigned long is usually the same as unsigned int, so it works. if you
want to shift 64bit value, you can use long long under gcc and __int64
(?) under msvc++.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Allan Rydberg写道:
Allan Rydberg wrote:
嗨
我想换一双,但是我收到了
错误消息''>>''非法,左操作数翻倍。
虽然这些联机帮助页说''>>''并且''<<''只能应用于int',我可以在unsigned longs上使用
shift运算符而没有任何问题。
有没有人知道如何正确地做到这一点?
即将字节单独复制到一个长数组中:
------------------------ --------
int指针;
char databuffer [100];
double uint64;
hi
i''m trying to shift a double, but i''m getting the
error message ''>>'' illegal, left operand double.
althought that the manpages say, ''>>'' and ''<<'' can
be applied for int''s only, i was able to use the
shift operator on unsigned longs without any problems.
does anyone know how to do this properly?
i.e. individually copying bytes into a long array:
--------------------------------
int pointer;
char databuffer[100];
double uint64;
double是浮点类型,不是整数类型
使用这些运算符进行移位只允许在整数类型上使用
(你可能想要查找64位整数类型你的平台)
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
double is floating point type, not integral type
shifting with these operators is allowed on integral types only
(you may want to look for a 64 bit integral type on your platform)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Allan Rydberg< al **** @ southtech.net>写道:
Allan Rydberg <al****@southtech.net> writes:
int指针;
char databuffer [100];
double uint64;
unsigned long uint32;
int pointer;
char databuffer[100];
double uint64;
unsigned long uint32;
double是浮点类型。
-
Philippe Amarenco,又名Phix
epita 2007 - LSE - EpX
"如果真正的新朋友没有受到明确私人联盟的保护,请打破
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
double is a floating point type.
--
Philippe Amarenco, aka Phix
epita 2007 - LSE - EpX
"if new true friend not protected for explicit private union, break
case and try using this." -- Nathan Myers, longest c++ sentence.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
这篇关于字节:移位双打?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!