473,664 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bytes: shifting doubles?


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++.m oderated. First time posters: Do this! ]

Nov 15 '05 #1
25 7883
Allan Rydberg wrote:
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 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++.m oderated. First time posters: Do this! ]

Nov 15 '05 #2
Allan Rydberg wrote:
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 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++.m oderated. First time posters: Do this! ]

Nov 15 '05 #3
Allan Rydberg <al****@southte ch.net> writes:
int pointer;
char databuffer[100];

double uint64;
unsigned long uint32;


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++.m oderated. First time posters: Do this! ]

Nov 15 '05 #4

Allan Rydberg wrote:
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?

Longs (and chars and shorts) are all integers. Doubles are floats, and
you cannot apply the shift operators to them. Just multiply or divide
by the appropriate power of two instead - instead of "a<<6" do "a*64".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Nov 15 '05 #5

Allan Rydberg wrote:
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;


A double is a double float. It is not an integer type, so it cannot be
bit shifted. (It can be divided by a power of 2, of course, but
presumably the bit patterns and not the values stored in dataBuffer are
of interest here).

A long long is an integer type and can be bit shifted.

So I recommend declaring uint64 like this:

typedef unsigned long long uint64;

for the code to compile as written. I would also recommend renaming
"pointer" since it is not a pointer, but an index.

Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Nov 15 '05 #6
Allan Rydberg wrote:
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?
actually the phrase is "...shift operators ... operands shall be
of integral or enumeration type". unsigned long is integral,
double is. You can't do this.
i.e. individually copying bytes into a long array:
--------------------------------
int pointer;
char databuffer[100]; unsigned char databuffer[100];
double uint64;
unsigned long uint32;

pointer =0;
well you could try this:

unsigned char *p;
p = (unsigned char*)&uint64; /* that's an odd name for a double...
*/
for (i = 0; i < sizeof(double); i++)
databuffer [i] = *p++;

this trick of copying one type to another via a cast pointer only
works for unsigned char. Anything else yields undefined behaviour.
Also note that different implementations may represent doubles
differently and hence the above code won't put the same thing in
databuffer.
// this works:
uint32 = 1111111; // anything
databuffer[pointer++] = uint32 >> 24;
databuffer[pointer++] = uint32 >> 16;
databuffer[pointer++] = uint32 >> 8;
databuffer[pointer++] = uint32 >> 0; // jup, i know..!
Undefined Behaviour
// 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;

--
Nick Keighley

"There are 10 types of people in this world.
Those that understand Binary, and those that don't".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Nov 15 '05 #7
A simple way to do that is to use a union and put both your double and
a char pointer variable or a char array of size double. Thats it. write
data in double and read the value from double but when to stream data
in and out of the char buffer get the data from char poiinter.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Nov 15 '05 #8
Rob
Allan Rydberg wrote:

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?
You've answered your own question. The basic >> and << operators are
only defined to work with integral types.


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;


If you want access to the bits in the double you might try doing a bit
of wizardry with casting of pointers;

unsigned long long *address; // double is (typically) longer than a
long

address = (unsigned long long)(&uint64);

databuffer[pointer++] = (*address) >> 56;

That will give you access to the bits that (internally) make up the
double variable.

The value of doing this is .... questionable. You might try
specifying what you're *really* trying to achieve by doing this: there
is almost certainly a better way to do it.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Nov 15 '05 #9
Allan Rydberg wrote:
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.
The operators are defined for all integral types, not just int.
But becareful with >> on a signed integral type; I tend to only
use it with unsigned types, just to be on the safe side.
does anyone know how to do this properly? i.e. individually copying bytes into a long array:
--------------------------------
int pointer;
char databuffer[100]; double uint64;
Now that's a confusing name.
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;


I suppose that you are formatting binary data for output. And
that the specification requires IEEE double, high byte first,
and that you accept the fact that your code will not be portable
to a machine which doesn't use IEEE floating point (IBM or
Unisys mainframe, for example). Given that you don't really
have absolute portability anyway, the obvious answer here is to
use a reinterpret_cas t, e.g.:

double d = 111111 ;
unsigned int const& ui
= reinterpret_cas t< unsigned int const& >( d ) ;
databuffer[ pointer ++ ] = ui >> 56 ;
// ...

If you require absolute portability, so that the code will run
as is even on a Unisys mainframe, you've got your work cut out
for you -- you have to functions like frexp to extract the
necessary information from the floating point format and create
your own IEEE format. But portability to this degree typically
isn't necessary.

--
James Kanze mailto: ja*********@fre e.fr
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3278
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not 'sizeof(char)'. It seems that I could use bitset<8> to represent a byte in my code --- if you have a better suggestion, I welcome it --- but that still leaves me with the question of how to write those bitsets to an image file as big-endian bytes...
25
6690
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual bytes. I've tried using bitwise operators, but they seem to convert the value into an integer first, and i've tried using the toString() method to convert it into a hex value so i can parse it, but that also seems to first convert it into an...
19
2103
by: James Harris | last post by:
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields the number of BYTES required to store an object of the type of its operand. What happens if C is running on a machine that addresses larger words only? Shouldn't sizeof be defined to return the smallest number of 'storage units' required to store an object of the type of its operand? As a general point, is there a guide to what aspects of C would fail if run on a...
3
958
by: Dan | last post by:
Is there a way to place x number of bytes directly into a structure? Specifically with this structure: Private Structure HeaderVersion1 Dim intVersion As Int16 Dim intCount As Int16 Dim intSysUptime As Int32 Dim intUnix_Secs As Int32 Dim intUnix_nsecs As Int32 End Structure
6
2181
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char. So, I can just assign an int to a char to retrieve the rightest byte of that int, and I also use this method plus right-shift operation to get the leftest byte of an int.
4
4711
by: Lee Crabtree | last post by:
I need to shift all of the values in a byte array by more than 8 bits, meaning that values should flow from one byte to another. Since I don't know in advance how many bits will be shifting, I can't do something easy like putting the bytes into a long or uint and shifting that. Let me give an example: If I want to shift this: 10010110 00001101
34
13904
by: john | last post by:
If I have a 32 bit unsigned int that is in the wrong byte order, how can I convert it? For example, if have a number 0x090a0b0c how can I reverse this to 0x0c0b0a09 ? Thanks, -John
9
2375
by: Noel Milton | last post by:
Hi: Ok, I've just read in up to 65,535 bytes into a char array (using the recvfrom socket API call). So I have an array of 8 bit char's (char recvString;). Now, four (4) consecutive bytes somewhere within that array represent a counter. How can I take those 4 individual bytes, concatinate the 32bits that
49
2692
by: seema | last post by:
Hi all, How to calculate upper 4 bytes and lower 4 bytes of a value? Can somebody please explain?? say for example, unsigned int scnlen_lo; int scnlen_hi; unsigned long long scnlen; scnlen=888888;
0
8437
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7375
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.