![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
int main()
{
union
{
short s;
char c[sizeof(short)];
}
un.s=0x0102
if(sizeof(short)==2)
{
if(un.c[0]==1 && un.c[1]==2)
printf("big-endian");
else if(un.c[0]==2 && un.c[1]==1)
printf("little-endian");
else
printf("unknown");
}
return 0;
}
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#12 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The "unknown" might seem strange, but there are actually more than two types of endianness. In-between types were more common in the days of 16-bit processors working with 32-bit values. Zilog, Intel, and some implementations of Motorola micros were all different.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#13 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
With regards to the original question, if you need a 64 bit type that you can use << and >> with, try "long long" under gcc and "__int64" if you are using Microsoft C
|
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Also, with regard to the original question, a couple of points that have come up in this thread, and a few other points;
1) floating point types (float and double) are not represented by two integers (of any type). Internally, floating point types are required to represent a value in the form sign*mantissa*10^exponent where (ONLY for sake of discussion here) ^ represents exponentiation, sign is +/- 1, mantissa is a real value in the range [0,1], and exponent is a signed integral value. In most floating point formats, sign is represented by a single bit, mantissa is represented by some number of bits, and exponent by yet more bits. There is no requirement in the C or C++ standards that the mantissa or exponent be represented in two separate integral fields. IEEE floating point formats go further than the requirements of the C/C++ standards and allow representation of some "special values", such as NaN (Not A Number), infinity, and a couple of others that I can't recall offhand. Representing those formats requires bits separate from the bit fields that represent mantissa, sign, or exponent. 2) Bit manipulation operators are only defined to work for integral types. They are not required to work for floating point types. 3) There is no requirement that a double be represented using a 64 bit type. In fact, I know of at least one machine that represented a double using 80 bits and I've heard mention (which I haven't confirmed) of one machine that represented a double using 60 bits. 4) There is no requirement that floating point types be represented using IEEE formats. And there are some common systems that don't use IEEE formats. 5) IIRC (i.e. I may be corrected) the long long type is not specified in the 1989 C standard, but is in the C++ standard. It is, however, supported by a number of older C compilers (including gcc) as an extension. I'm not sure what the 1999 C standard specifies. 6) If you really want a 64 bit integral type, the way to be sure and have it work portably is to use some form of data structure that represents it for you. Use of long long will work with some compilers, but not all. __int64 will work with Microsoft C. In practice, unsigned char types are 8 bit or more, unsigned short types are 16 bits or more, and unsigned long types are 32 bits or more. Those are not specifically stated by the standard but the RANGE of values that must be supported in those types implies it (eg the maximum value of an unsigned char must be 255 or more, and that translates into 8 or more bits). So, you can represent a 64 bit type using an array of 8 unsigned chars, 4 unsigned ints, or 2 unsigned longs --- portably. Or use one unsigned long long, if you wish to run some risk of non-portability. In C++, the obvious way to implement that would be a class that supplies relevant operators (eg operator &, operator|, etc) that interact with the array of data used internally. In other words, do not use floating point types at all. One advantage of using the C++ class approach is that it is easily extendable to support much larger numbers of bits. |
|
|
|
|
|
#15 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
Thanks for all the info. I have decied to use a class of two unsigned long int's and overload the operators.
I did however learn a lot about float, which whould explain a lot of my problems I was having with some of my programs way back when I was a real beginner. Milton |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|