# left shifting signed quantity

• 03-31-2009
BEN10
left shifting signed quantity
hello all,

this is a quote from K&R pg no. 49
Quote:

right shifting a signed quantity will fill with sign bits ("arithmetic bits") on some machines and with 0-bits ("logical shift") on others.
is this true for left shift also.is left shifting a signed quantity also machine dependent.
also i wanna know how can i determine whether my system uses ASCII encoding or EBCDIC or some else encoding.my OS is windows xp.
Thank you
• 03-31-2009
matsp
left-shifting ALWAYS fills with zero. Note however that shifting left a sufficient number of bits will result in a negative number.

Right-shifts on x86 are available as both arithmetic and logical shifts, so the compiler can choose (and we hope that it gets it right!). But the standard allows a right-shift to be implemented by the compiler as either arithmetic or logical even if the value is a signed type.

I have no idea how you DETERMINE if your system uses EBCDIC or ASCII, but unless you are doing IBM 3270 terminal emulation (or some other similar thing for connecting to IBM mainframes), I can almost guarantee that your system is EBCDIC free.

--
Mats
• 04-01-2009
Maz
It should be possible determine this by casting a char to int, and comparing the value to one from ASCII chart or EBCDIC chart. (If I understood the EBCDIC correctly - I never knew about that before :) )
• 04-01-2009
matsp
Quote:

Originally Posted by Maz
It should be possible determine this by casting a char to int, and comparing the value to one from ASCII chart or EBCDIC chart. (If I understood the EBCDIC correctly - I never knew about that before :) )

You don't even need to do casting. You can do something like this:
Code:

if ('A' == 65)
{
printf("ASCII\n");
}
else if ((unsigned char)'A' == 193)
{
printf("EBCDIC");
}
else
{
printf("Unknown\n");
}

Of course, if that comes out as a bunch of goobledegook or "nothing" then you are using a compiler that generates ASCII codes on a machine that shows characters as EBCDIC or the other way around.

So if the problem is to find out what to set the compiler to use, then the above won't work, right?

--
Mats
• 04-01-2009
BEN10
i ran the above program and the output is "ASCII".so this means that my compiler as well as system uses ASCII encoding.or is it that the system may use some other encoding(this output only shows the compiler's encoding).
• 04-01-2009
matsp
Quote:

Originally Posted by BEN10
i ran the above program and the output is "ASCII".so this means that my compiler as well as system uses ASCII encoding.or is it that the system may use some other encoding(this output only shows the compiler's encoding).

Yes, unless you have a computer that sounds like a small jet engine and is the size of a fridge and cost like a large car, I'd expect it to be ASCII. Only IBM produce machines that use EBCDIC, and far from all IBM machines use EBCDIC. Such as AS/400 or Z-series machines. But an IBM PC or compatible, and nearly everything else, uses ASCII.

--
Mats
• 04-01-2009
BEN10
thanks matsp.