473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Defining a 32 Bit Integer on every platform?

Hi,

i want to write a chat program based on a self writen network
protocol. It should run on any platform so there is one problem: On
one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
one (for compatibly issues). It should be also possible to add and
subtract etc from this type... So how can i define this type?
Thx Frank
Jul 22 '05 #1
7 3524
Frank Tombe wrote:
Hi,

i want to write a chat program based on a self writen network
protocol. It should run on any platform so there is one problem: On
one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
one (for compatibly issues). It should be also possible to add and
subtract etc from this type... So how can i define this type?
Thx Frank


What about byte order ?
Anyhow, the usual way to solve this is to use a typedef that is defined
for each platform.

There are some tricks to make the byte-ordering also transparent, if
you're interested, just ask.

Jul 22 '05 #2
On 8 May 2004 08:29:22 -0700 in comp.lang.c++, nc******@freequote.net
(Frank Tombe) wrote,
i want to write a chat program based on a self writen network
protocol. It should run on any platform so there is one problem: On
one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
one (for compatibly issues). It should be also possible to add and
subtract etc from this type... So how can i define this type?


Does your 64bit platform offer any 32bit integer datatype at all?
No reason why it has to, but perhaps that platform is especially
unsuited to your requirements and should not be used.

Out of curiosity, what is it?

Compare <boost/cstdint.hpp> from http://www.boost.org

Jul 22 '05 #3
Gianni Mariani posted:
Frank Tombe wrote:
Hi,

i want to write a chat program based on a self writen network
protocol. It should run on any platform so there is one problem: On
one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
one (for compatibly issues). It should be also possible to add and
subtract etc from this type... So how can i define this type?
Thx Frank


What about byte order ?
Anyhow, the usual way to solve this is to use a typedef that is defined
for each platform.

There are some tricks to make the byte-ordering also transparent, if
you're interested, just ask.

I myself have done the following to get out of the byte-order problemo:
union Numbr
{
struct {
unsigned __int8 a;
unsigned __int8 b;
unsigned __int8 c;
unsigned __int8 d; }

unsigned __int32 ThirtyTwoBit;
};
Jul 22 '05 #4
JKop wrote:
Gianni Mariani posted:

....

There are some tricks to make the byte-ordering also transparent, if
you're interested, just ask.


I myself have done the following to get out of the byte-order problemo:
union Numbr
{
struct {
unsigned __int8 a;
unsigned __int8 b;
unsigned __int8 c;
unsigned __int8 d; }

unsigned __int32 ThirtyTwoBit;
};


I posted an answer a while ago - here is the google-groups cache of the
post.

http://tinyurl.com/2ffdw

Jul 22 '05 #5
Ian
JKop wrote:
Gianni Mariani posted:

Frank Tombe wrote:
Hi,

i want to write a chat program based on a self writen network
protocol. It should run on any platform so there is one problem: On
one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
one (for compatibly issues). It should be also possible to add and
subtract etc from this type... So how can i define this type?
Thx Frank


What about byte order ?
Anyhow, the usual way to solve this is to use a typedef that is defined
for each platform.

There are some tricks to make the byte-ordering also transparent, if
you're interested, just ask.


I myself have done the following to get out of the byte-order problemo:
union Numbr
{
struct {
unsigned __int8 a;
unsigned __int8 b;
unsigned __int8 c;
unsigned __int8 d; }

unsigned __int32 ThirtyTwoBit;
};

Whats wrong with using uint32_t and htonl/ntohl and friends?

Ian
Jul 22 '05 #6
Ian wrote:
JKop wrote:
....
Whats wrong with using uint32_t and htonl/ntohl and friends?


htonl/ntohl only works with 32 or 16 bit numbers and you have to cast
back and forth for types other than unsigned. class NetworkOrder (see
http://tinyurl.com/2ffdw ) work with any byte order specific type and
does the magic of applying the endian translations for you, not to
mention that on a platform that has the right endianness, it will be
pretty fast compared to a function call.

Jul 22 '05 #7
Ian
Gianni Mariani wrote:
Ian wrote:
JKop wrote:

...

Whats wrong with using uint32_t and htonl/ntohl and friends?

htonl/ntohl only works with 32 or 16 bit numbers and you have to cast
back and forth for types other than unsigned. class NetworkOrder (see
http://tinyurl.com/2ffdw ) work with any byte order specific type and
does the magic of applying the endian translations for you, not to
mention that on a platform that has the right endianness, it will be
pretty fast compared to a function call.

On a platform with the right endianness, htonl/ntohl will be #defined to
do nothing, which is faster still.

Never mind, I see your point.

Ian

Jul 22 '05 #8

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

Similar topics

5
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
14
by: David Fisher | last post by:
The most common sizes of integer types seem to be: 8 bits - signed char 16 bits - short 16 or 32 bits - int 32 or 64 bits - long Question #1: Does anyone know how common sizes other than...
13
by: Howard | last post by:
Hi, I know I can do integer division with the / operator, and get the modulus with the % operator, but is there any function that calculates both values in one shot? It seems quite wasteful in...
18
by: Ptp | last post by:
is there a integer data type of one byte in gcc? I know delphi have fundamental integer types include byte, shortint... the byte types is unsigned 8-bit, such as below: procedure...
6
by: ESOJAY | last post by:
Is "typedef long long int64" the best way to define an int64 type in c? Are there better alternatives? Thanks, ESOJAY -- ESOJAY
1
by: Chua Wen Ching | last post by:
I have a question to ask. Please give me your comments and see whether this concept is possible and feasible or not? I want to come out with a concept maybe a library that can be used by all...
61
by: John Baker | last post by:
When declaring an integer, you can specify the size by using int16, int32, or int64, with plain integer being int32. Is integer the accepted default in the programming community? If so, is...
1
by: =?Utf-8?B?U2VhbiBDb25uZXJ5?= | last post by:
Hi, Is it possible to define DllMain in a static library for use in dlls? The reason is that I am defining a platform abstraction for being loaded as a shared library and I figured the simplest...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...

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.