WriteInt(long int i) 64bit

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

Posts: 25
Joined: 23 May 2016, 03:59

WriteInt(long int i) 64bit

Postby zynbasil » 23 May 2016, 04:02

Xcode 64bit

Code: Select all

// -------------------------------------------------------------------
// WriteInt
// -------------------------------------------------------------------
void ByteArray::WriteInt(long int i)

   unsigned char bytes[4];

   bytes[0] = (i >> 24) & 0xFF;
   bytes[1] = (i >> 16) & 0xFF;
   bytes[2] = (i >> 8) & 0xFF;
   bytes[3] = i & 0xFF;

   boost::shared_ptr<vector<unsigned char> > buf (new vector<unsigned char>());

why long int

when 64bit sizeof(long int)=8
User avatar
Site Admin
Posts: 19988
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: WriteInt(long int i) 64bit

Postby Lapo » 23 May 2016, 07:32

thanks for reporting. I've forwarded your question to our C++ expert.

Stay tuned.

p.s. = used the "Code" button on top of the text area to format code and keep the formatting. Thanks!
...addicted to flash games
Posts: 125
Joined: 12 Feb 2013, 10:57

Re: WriteInt(long int i) 64bit

Postby MBagnati » 27 May 2016, 05:50

thanks for reporting.

Let me spend few words about "int" and "long int" to explain why WriteInt function declares its input parameter using the "long int" type.
Different pages of the C++ literature define "int" and "long int" in different ways.
Someone (for instance Microsoft at the url https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx) says that both "int" and "long int" are 4-bytes long.
Someone (for instance iOS at the url https://developer.apple.com/library/ios ... anges.html)
says that "int" is always 4-bytes long, while "long int" is a 4-bytes data type in 32bit enviroment but grows to 8-bytes data type in 64bit platform.
Someone (for instance url http://www.cplusplus.com/doc/tutorial/variables/) says that "int" as a size of at least 16-bits while "long int" is at least 32-bits long.

Taking in mind these pages and that WriteInt function needs of a 32bit value as input parameter, we have to choose a data type that is suitable for the largest amount of systems.
The "int" data type could be the obvious choice but sometimes it is defined as "at least 16 bits". This definition does not assure that it is always able to contain a 32bit value.
For this reason our choiche is "long int".
With the usage of "long int" we have the reasonable certainty that "it is at least 32 bits" everywhere...yes, it could be greater of 32bits on some systems but it is everywhere suitable to contain a 32bit value.

So, may be that in some systems (like iOS 64bit where "long int" is defined as 8-bytes data type) the container for the parameter of WriteInt will be bigger than the necessary...never mind, it means that it will use a larger box than necessary
In any case the WriteInt function uses only the less significant 32bits of provided parameter, therefore a container larger than necessary does not create anomalies
Posts: 25
Joined: 23 May 2016, 03:59

Re: WriteInt(long int i) 64bit

Postby zynbasil » 27 May 2016, 10:17

clear,thank you very much!!

Return to “SFS2X C++ API”

Who is online

Users browsing this forum: No registered users and 1 guest