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?
/*____________________________________________________________________________** limits.cpp - provides the limits of the primitive data types on* the machine it is running, plus more.** Copyright (c) 2007 Georgi Todorov** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,* USA.** Created on: 02 May 2007* Author: Georgi Todorov* E-mail: terahz <at> geodar <dot> com*____________________________________________________________________________*/#include <iostream>using namespace std;int endian(){ // 0 is Little endian, 1 is Big endianshort int word = 0x0001;char *byte = (char *) &word;if (byte[0] == 1){return 0;}else if (byte[0] == 0){return 1;}}int main (void) {int integer = 1;int intbits = 0; // size of intlong longint = 1;int longbits = 0; // size of longfloat floa = 0;int *pfloat = (int*)(&floa);*pfloat = 1;int floatbits = 0; // size of floatdouble doub = 0x1;int *pdouble1 = (int*)(&doub);int doublebits = 0; // size of doubleint *pdouble2;pdouble2 = pdouble1++;*pdouble2 = 1;while ( integer != 0 ){integer = integer << 1;intbits ++;}cout << "int type is " << intbits << " bits long" << endl;while ( longint != 0 ){longint = longint << 1;longbits ++;}cout << "long type is " << longbits << " bits long" << endl;while ( (*pfloat) != 0 ){(*pfloat) = (*pfloat) << 1;floatbits ++;}cout << "float type is " << floatbits << " bits long" << endl;while ( (*pdouble2) != 0 ){(*pdouble2) = (*pdouble2) << 1;doublebits ++;}cout << "double type is " << doublebits*2 << " bits long" << endl;floa = 0.15625;if ( ((*pfloat) ^ 0x3E200000) == 0 ){cout << "Single-precision binary floating-point number representation is IEEE 754" << endl;;}else{cout << "Single-precision binary floating-point number representation is NOT IEEE 754" << endl;}doub = 1;if ( ((*pdouble1) ^ 0x3FF00000) == 0 ){cout << "Double-precision binary floating-point number representation is IEEE 754" << endl;;}else{cout << "Double-precision binary floating-point number representation is NOT IEEE 754" << endl;}if ( !endian() ){cout << "Little Endian " << endl;}else{cout << "Big Endian" << endl;}}- Свалете този код/download this code: limits.cpp
Toshe
on 2007-04-13
Under what version of GPL is this program licensed? V.1, v.2 or “v.2 and later”
TeraHz
on 2007-04-13
I will put the full copyright info in the file right now. Thanks for pointing out this to me
Дали мога да накарам полиглот-а да работи и за коментари? Понеже постингите са на 2 езика и виждам, че някои хора отговарят на Български а някои на Английски…
Toshe
on 2007-04-15
Ами не съм сигурен за плъгина. Аз лично ако съм от работа пиша на английски, иначе - на български. Но не мисля, че за повеето от четящите блога двуезичието е проблем