What is a better play with bits and pointers than figuring out the size of the basic data types, weather floating point arithmetic is IEEE 754 and what is the endian of the machine we are on?

  1. /*____________________________________________________________________________
  2. *
  3. * limits.cpp - provides the limits of the primitive data types on
  4. * the machine it is running, plus more.
  5. *
  6. * Copyright (c) 2007 Georgi Todorov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  21. * USA.
  22. *
  23. * Created on: 02 May 2007
  24. * Author: Georgi Todorov
  25. * E-mail: terahz <at> geodar <dot> com
  26. *
  27. ____________________________________________________________________________*/
  28.  
  29. #include <iostream>
  30.  
  31. using namespace std;
  32.  
  33. int endian(){ // 0 is Little endian, 1 is Big endian
  34. short int word = 0x0001;
  35. char *byte = (char *) &word;
  36. if (byte[0] == 1){
  37. return 0;
  38. }else if (byte[0] == 0){
  39. return 1;
  40. }
  41. }
  42.  
  43. int main (void) {
  44. int integer = 1;
  45. int intbits = 0; // size of int
  46. long longint = 1;
  47. int longbits = 0; // size of long
  48. float floa = 0;
  49. int *pfloat = (int*)(&floa);
  50. *pfloat = 1;
  51. int floatbits = 0; // size of float
  52. double doub = 0x1;
  53. int *pdouble1 = (int*)(&doub);
  54. int doublebits = 0; // size of double
  55. int *pdouble2;
  56. pdouble2 = pdouble1++;
  57. *pdouble2 = 1;
  58.  
  59.  
  60. while ( integer != 0 ){
  61. integer = integer << 1;
  62. intbits ++;
  63. }
  64. cout << "int type is " << intbits << " bits long" << endl;
  65.  
  66. while ( longint != 0 ){
  67. longint = longint << 1;
  68. longbits ++;
  69. }
  70. cout << "long type is " << longbits << " bits long" << endl;
  71.  
  72.  
  73. while ( (*pfloat) != 0 ){
  74. (*pfloat) = (*pfloat) << 1;
  75. floatbits ++;
  76. }
  77. cout << "float type is " << floatbits << " bits long" << endl;
  78.  
  79. while ( (*pdouble2) != 0 ){
  80. (*pdouble2) = (*pdouble2) << 1;
  81. doublebits ++;
  82. }
  83. cout << "double type is " << doublebits*2 << " bits long" << endl;
  84.  
  85. floa = 0.15625;
  86. if ( ((*pfloat) ^ 0x3E200000) == 0 ){
  87. cout << "Single-precision binary floating-point number representation is IEEE 754" << endl;;
  88. }else{
  89. cout << "Single-precision binary floating-point number representation is NOT IEEE 754" << endl;
  90. }
  91. doub = 1;
  92.  
  93. if ( ((*pdouble1) ^ 0x3FF00000) == 0 ){
  94. cout << "Double-precision binary floating-point number representation is IEEE 754" << endl;;
  95. }else{
  96. cout << "Double-precision binary floating-point number representation is NOT IEEE 754" << endl;
  97. }
  98.  
  99. if ( !endian() ){
  100. cout << "Little Endian " << endl;
  101. }else{
  102. cout << "Big Endian" << endl;
  103. }
  104.  
  105. }
  106.