[MUSIC].
Okay, let's talk a little bit about how
we'd handle unsigned and signed integers
in the C language.
which we'll be using in our first
homework assignment.
So as I mentioned in the previous video
the numbers that we can represent in both
unsigned and two's complement notation
have maximum and minimums.
And what we have here is a table that
shows what those maximum and minimum
numbers are for various sizes of the
word.
so W of eight, 16.
32 and 64 and you can see the, the, the
basic property is that the range of the
two's compliment numbers is pretty much
symmetric, it turns out there's.
one more negative number then positive
numbers, but thats pretty close, and in
the case of unsigned numbers the maximum
number we can represent is about twice
the size of the maximum twos compliment
positive, C provides a port for unsigned
and signed numbers.
In a file called limits .H, .H files are
header files and they can be include din
our own code by statements such as the
one above.
You'll see these come up in some of the
sample code that we'll be doing for the
homework assignments.
But in limits .H for example we have some
constanst that are predeclared for us for
the various Maximum and minimum values
for both a, standard word size and the
long linuxers.
And these values of platforms specific.
So we'll be using limits that age that's
on a linux system.
Of course, if your on a 64 bit linux
machine, things will represent, will be
more in the 64 bit a values.
If you're on an older machine, you might
see the 32 bit values.
Okay, so in c, constants are by default
considered to be signed integers.
So a number, if we declare an int, it's a
signed integer.
If we want it to be unsigned, we have to
explicitly say so We can also when we
just write a number to say that we mean
it to be represented as an unsigned
integer, we can just add a capital U to
the end of it.
So number such as these are are,
unsigned.
Okay so in the C language again into.
is for integer and that means a signed
integer.
If we want unsigned we use just the word
unsigned.
and then we can cast back and forth
between these numbers.
So we can take an unsigned integer, cast
it as an integer as a signed integer and
assign it to a regular integer variable.
And vice versa.
Implicit casting also occurs.
So we don't have to put the, explicit,
way we want the number interpreted in
parentheses.
And this happens both in assignments and
in function calls when you use these
variables as, arguments to a function.
so here we have an unsigned value being
assigned to a signed value, a signed
value being assigned to an unsigned
value.
Now, what happens in the compiler when
this goes on is it basically will figure
out how to move that number not actually
move it, but reinterpret it.
As the corresponding unsigned or signed
integer, Okay?
But lets take a look in more detail on
what actually happens here.
We're going to look at a set of examples.
the key thing to remember is the bits are
actually unchanged, we're just going to
interpret them differently.
Okay.
So, if you mix unsigned and signed values
in a single expression, then signed
values get implicitly cast to unsigned.
So unsigned dominates, if you will.
So let's take a look at what happens here
in some examples.
Suppose I have the constant zero, which
is a signed value.
And the constant 0U, an unsigned value.
Well, these these values are exactly the
same, but they're both evaluated as if
they were unsigned.
In other words, this signed 0 is being
reinterpreted as an unsigned number.
Fortunately, zero is all, is all the bits
are zero in either case.
So that's pretty straight forward to do.
Let's take a look at the next one.
minus one unsigned.
I'm sorry, minus one signed and zero
signed.
Well in this case both numbers are
signed, so we will do the evaluation of
signed numbers.
And we would expect in fact the minus 1
to be less than, interpreted as being
less than 0.
In terms of the bit patterns here,
though, remember the minus 1 is all ones.
The zero is all zero.
But it's going to be interpreted as a
signed value, so that first bit of the
representation of minus 1 has that high
negative weight.
Which will make it less than 0.
Okay.
Now, if instead we compare the -1 to an
unsigned 0 in the next line, that's a
little different because now we're going
to reinterpret the -1 as an unsigned
value.
Well, all 1s reinterpreted as an unsigned
value is a huge number.
Because we lost that negative weight of
that first The most significant bit.
So, now we have a large number and it's
going to come out as being greater than.
So, look at that.
Just because one of the constants is
interpreted as an unsigned number, we get
a totally different result.
even though we might think we're
comparing the same values.
But that's just because of how the
numbers are being interpreted.
And C has this convention of converting
everything to unsigned interpretation if
the, if we mix the two types.
OK, let's take a look at a different
example in the next line.
We're using here a large positive number
as a signed integer.
And comparing it against a very small
negative number.
And, here again, both are signed values.
We'll interpret them as signed.
And of course that large positive is
greater.
Now, again, if one of them is interpreted
in fact as unsigned, what happens?
Well, the big negative number is also
going to be converted to unsigned, and
what does it become?
it becomes an even bigger positive
number, so now it the comparison will say
that the number on teh left which looks
like a positive number is less than this
supposedly negatvie number but this
negative number was reinterpreted.
As an unsigned value and turned into a
big positive number.
Okay.
Next example, minus one against minus
two, both signed, we would expect the
minus one to be greater than the minus
two.
in the next example a minus one cast as
unsigned again yields a huge positive
number.
It is going to be greater than the minus
two.
So in this case it happens to work out,
that we get the same result.
But again that's only because of the
interpretation, kay.
Lets take a look at the last two lines.
Here we have a large positive sign
number.
Compared against a large positive
unsigned number.
What will happen here is that that number
on the left will be converted to unsigned
as well.
Will still be a relatively large positive
number and still smaller than the one on
the right.
So here we would expect less than
unsigned.
Now, if instead we cast that.
Unsigned number as an integer first,
reinterpret the bits.
Well, that number turns out is just large
enough to have that most significant bit
set to one.
So when we interpret it as an integer,
it's going to look like a negative
number.
And in this casek we would expect the
opposite result when we compare them as
signed values.
Wyszukiwarka
Podobne podstrony:
03 3 sitowa i inForgotten Realms Mysteries, 03 Murder in Halruaa (v0 9)03 In the BEGINNING863 03E in T?atures & nescessityFunctional Origins of Religious Concepts Ontological and Strategic Selection in Evolved MindsYou maybe in love Blue CafeIn the?rnALL L130310?lass101Mode 03 Chaos ModeGhost in the Shell 2 0 (2008) [720p,BluRay,x264,DTS ES] THORAwięcej podobnych podstron