Jump to content

Casting between integers

- - - - -

  • Please log in to reply
6 replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hei,

I'm working with libraries of different kinds and also building some
C-wrappers for DLLs which .NET-programs can then use. I'm constantly dealing
with different kinds of integers. I'm aware that an unsigned one needs to be
converted when a signed one is wanted, but how about different kinds of
signed? I'm using at least the following: int, boost::int32_t, INT and INT32
(windows types). Some of these are mere typedefs and I assume that they're
used in order to get some consistency or something, but the question remains:
do I have to explicitly cast between them? Does the type of cast (C-style,
C++-styles etc) matter?

Any input is welcomed.

Thanks.

Ps. I'm working with c++ in windows environment.

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
The important thing to know whether a type cast actually affects your values or not for e.g.

if you have a type which is uint16 and it is assigned a uint32, then definitely your variable is going to loose part of the value. So this needs to be looked into. However the converse would have no problems since a 32 bit number can contain a 16 bit number.

Other thing is when ever you have different types, you are going to see warnings of conversions. If your warning level is high, even errors. So you need to fix those all too.

Every place you have a warning and you know it is okay to convert, you have to cast even if it is only a warning since any decent code shouldnt have any warnings.

#3
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
So assuming that the different varieties of 32-bit integers are constructed in the same way, casting is unnecessary and casting between 16-bit and 32-bit integers requires some consideration. This raises a question: what exactly does a C-style cast do?

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Answer to the first part of your question is yes.

Second part i am not certain i understand you. C style cast (assuming implicit cast where cast operator is not needed() )is simply attempting to place the right side into the left side. If it can do that without losing any data it accepts that without a warning. If it can't it gives warning in certain cases if it is only loss of data and errors if it just cant copy.

Explicit case done with (<type name>) does the conversion even if there is loss of data since it assumes programmer knows what it is doing. This is done successfully unless compiler really has no clue how to convert right side into left.

There is no awareness like that of c++ dynamic, static, reinterpret cast etc in c, which could identify for e..g if a pointer of different type is assigned to another type. So c is less powerful in terms of type checking than c++.

#5
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

fayyazlodhi said:

Second part i am not certain i understand you.

My bad. I should've been more explicit. I can understand that compilers know how
to convert from int to double and also the other way with casting. The compiler
has the appropriate knowledge to do that. Am I to assume from this that casting is
in fact a series of conversions from one type to another, only behind the scenes.
I'm thinking that's not it. This is what I was referring to, what does casting do exactly ?
How does compiler know which bits to copy and in which way? Is it a straight up binary
copy ?

#6
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Yeah that clarifies much of the question. Though i am not fully aware of compiler implementations but i can explain to some extent.

Mostly it happens behind the scenes like this:


int i, float f, char c;


i = 10;

f= 35.5;

c = 'b';


int result = c + (i + f); // ignore the down cast error (if any) for now when converting to c.


First, compiler picks the higher data type which is float and converts i into float, then it sums i and f and store their result into another temporary float variable.
Then it sees the next type (which is character) creates a new temp float (because float is higher among the two) and assign value of c into that, then it adds the previous result from temp (result of i and f), adds the converted value of c to that and proceeds to next step.

Compiler definitely needs special instructions of some sort to convert for e.g. an int into float because they are stored differently. Some reference of that instruction is present in the below mentioned thread. But the point is integer is stored like if 2 bytes, it has one Most Significant Byte and one Least significant byte whose individual value is in a single byte.

Compared to that for a float of four bytes, it's 3 bytes are reserved for integral part and the fourth byte contains exponent. So there is definitely a standard conversion to and from routine which would be called every time needed.

Following thread might be helpful
internals of typecasting - C / C++

#7
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Thank you for your helpful answers .. I think I got the point well enough ..




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users