Erm, check something for me

Posts 1–15 of 16 · Page 1 of 2
Erm, check something for me
Just doing some theory refresh , and I won't be home for like another week /:

So please check this if I failed or anything for me xD

Code:
int i = 65537;
short s = i;
s becomes -32767 yeah?

Code:
int i = 2097217;
char c = i2;
c becomes "A"?

Thanks !
First one i get 1

Second thing i dont know what Operator you want between i and 2
Quote Originally Posted by whit View Post
First one i get 1 :(

Second thing i dont know what Operator you want between i and 2
Thanks whit!

Do them in C / C++ :)

They don't need any operators (or.. shouldn't). Simply copy-paste them :D
Quote Originally Posted by freedompeace View Post


Thanks whit!

Do them in C / C++

They don't need any operators (or.. shouldn't). Simply copy-paste them
Yea you need operator ( +, -, *, / ect) im using visual Studio 2010
idk what you using where you dont need a operator b/w the two
Yeah the first one prints out 1, The second one "i2" Isn't in scope.
In math, when a variable is right next to a number like that it's normally multiplication or it means two of said variable. Not sure which he means though, or if I'm even right. o:
Quote Originally Posted by t7ancients View Post
In math, when a variable is right next to a number like that it's normally multiplication or it means two of said variable. Not sure which he means though, or if I'm even right. o:
If you do

Code:
    int i = 2097217;
    char c = i * 2;
It prints out é. :P
My bad,

Code:
int i2 = 2097217;
char c = i2;
Quote Originally Posted by freedompeace View Post
My bad,

Code:
int i2 = 2097217;
char c = i2;
Well this does in fact print out A
Quote Originally Posted by freedompeace View Post
My bad,

Code:
int i2 = 2097217;
char c = i2;
Oh i see what you were doing there xD Yeah it works now :P
thanks guys
hopefully I'll win this contest
K instead of letting the compiler do the work why don't you explain WHY those values become 1 and 'A' by hand and get some extra kudos for whatever assignment this is Anyone can observe & write down some results, but understanding why & how the results are generated is the more important part.

65537 in Binary:
0000 0000 0000 0001 0000 0000 0000 0001
Casting to a short (16 bits/2 bytes) we drop the first 16 bits so you end up with
0000 0000 0000 0001 = 1

2097217 integer in binary is:
0000 0000 0010 0000 0000 0000 0100 0001
When you cast it to a char (which is a signed byte) you take the 8 LS bits, which are 0100 0001. This is 65 Decimal/0x41 which is the value of 'A' in the ASCII table.
Quote Originally Posted by B1ackAnge1 View Post
K instead of letting the compiler do the work why don't you explain WHY those values become 1 and 'A' by hand and get some extra kudos for whatever assignment this is Anyone can observe & write down some results, but understanding why & how the results are generated is the more important part.

65537 in Binary:
0000 0000 0000 0001 0000 0000 0000 0001
Casting to a short (16 bits/2 bytes) we drop the first 16 bits so you end up with
0000 0000 0000 0001 = 1

2097217 integer in binary is:
0000 0000 0010 0000 0000 0000 0100 0001
When you cast it to a char (which is a signed byte) you take the 8 LS bits, which are 0100 0001. This is 65 Decimal/0x41 which is the value of 'A' in the ASCII table.
Oh! Thank you B1ackAnge1! I did a miscalculation (a fuck up) and started from 2^1 rather than 2^0.

If we were to have ...
0000 0000 0000 0000 1000 0000 0000 0001, assigning that to a short would turn it into -32767?
Quote Originally Posted by freedompeace View Post


Oh! Thank you B1ackAnge1! I did a miscalculation (a fuck up) and started from 2^1 rather than 2^0.

If we were to have ...
0000 0000 0000 0000 1000 0000 0000 0001, assigning that to a short would turn it into -32767?
yup - of course what'll really bake your noodle is - Did the original question describe if this was supposed to be on a big-endian or little endian computer ? would give you quite a different output
Quote Originally Posted by B1ackAnge1 View Post
yup - of course what'll really bake your noodle is - Did the original question describe if this was supposed to be on a big-endian or little endian computer ? ;) would give you quite a different output
Well the contest hasn't started yet (doesn't until the weekend), I just have a feeling that something like this will probably be on it. After all I'm going to be dominated by people of your caliber :(

I've always assumed it was little-endian (since it was easier), but if it was big-endian, the same series: 0000 0000 0000 0000 1000 0000 0000 0001 to a short would be:

reverse byte order = 0000 0001 1000 0000 0000 0000 0000 0000
to sizeof short = 0000 0000 0000 0000
= 0?

-------------------------------

Taking a more "complex" example: 0000 0011 0100 1111 1000 1000 0000 0001
reverse byte order = 0000 0001 1000 1000 0100 1111 0000 0011
to sizeof short: 0100 1111 0000 0011
complement: 1011 0000 1111 1100
subtract 1 = 1011 0000 1111 1011
= 12539?

I'm not sure about these ones, so any help would be awesome C:

(More verbose than it needs to be, but in case I fuck up, it'll (hopefully) be easier to see where I went wrong)
Posts 1–15 of 16 · Page 1 of 2

Post a Reply

Tags for this Thread

None

Talk with us