GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-May-2008, 00:29
meili100 meili100 is offline
Junior Member
 
Join Date: Feb 2007
Posts: 37
meili100 has a little shameless behaviour in the past

How to tell a type is signed or unsigned?


CPP / C++ / C Code:
typedef int mytype;
//typedef unsigned int mytype;

bool isSigned(mytype x)
{
	//how to tell if mytype is a signed type or not?
}

Suppose I have mytype that could be defined as either signed (int, short, long) or unsigned. And I don't know what the type is passed in.
How do I tell if mytype is a signed type or not?
Last edited by LuciWiz : 09-May-2008 at 08:15. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 05-May-2008, 08:39
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How to tell a type is signed or unsigned?


You could set it to a negative value, then read it and see if it is still negative.
  #3  
Old 05-May-2008, 08:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to tell a type is signed or unsigned?


Quote:
Originally Posted by meili100
[
How do I tell...

If you declare that mytype is an int, the function treats it as an int.
If you declare that mytype is an unsigned int, the function treats it as an unsigned int.

If the calling program has an argument that is not the same type as the declared function, the compiler converts it if necessary.

Note that casting an int to an unsigned int (or vice versa) doesn't actually change the bits stored in memory, but that's irrelevant to your question: the function just gets a value from the calling program and the parameter type (inside the function) is defined by the function declaration.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

typedef          int mytypeSi;
typedef unsigned int mytypeUi;

void printmyTypeSi(mytypeSi x)
{
    cout << "In printmyTypeSi: x  = " << x << endl;
}
void printmyTypeUi(mytypeUi x)
{
    cout << "In printmyTypeUi: x  = " << x << endl;
}



int main()
{
    int       i = -1;
    unsigned ui = i;
    float    fi = -123.45;

    cout << "In main         : i  = " << i << endl;
    printmyTypeSi(i);
    printmyTypeUi(i);
    cout << endl;

    cout << "In main         : ui = " << ui << endl;
    printmyTypeSi(ui);
    printmyTypeUi(ui);
    cout << endl;

    cout << "In main         : fi = " << fi << endl;
    printmyTypeSi(fi);
    printmyTypeUi(fi);
    cout << endl;


    return 0;
}

Output
Code:
In main : i = -1 In printmyTypeSi: x = -1 In printmyTypeUi: x = 4294967295 In main : ui = 4294967295 In printmyTypeSi: x = -1 In printmyTypeUi: x = 4294967295 In main : fi = -123.45 In printmyTypeSi: x = -123 In printmyTypeUi: x = 4294967173

Regards,

Dave
Last edited by davekw7x : 05-May-2008 at 09:49.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
BMP image on matrix load nemo C++ Forum 21 18-Dec-2007 00:04
Linker Error athika_32 C Programming Language 6 03-Dec-2007 08:38
[Linker error] undefined reference to `WinMain@16' squid C++ Forum 5 02-Mar-2007 23:58
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 20:23
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:20.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.