Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 11th, 2006, 7:22 PM   #11
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by ionexchange
#define LITTLE_ENDIAN 0
#define BIG_ENDIAN    1

int main()
{
   float i = 1;
   char *p = (char *) &i;
   if (p[0] == 1) // Lowest address contains the least significant byte
      std::cout << "little" << std::endl;
   else
      std::cout << "big" << std::endl;
return 1;
}
Here's a better way of finding endianness(Copied from "Unix Network Programming" by W.R. Stevens):
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.
InfoGeek is offline   Reply With Quote
Old Feb 11th, 2006, 7:37 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 11th, 2006, 7:50 PM   #13
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
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
The Dark is offline   Reply With Quote
Old Feb 12th, 2006, 3:57 AM   #14
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Feb 12th, 2006, 9:27 AM   #15
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
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
ionexchange is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC