left shifting signed quantity

This is a discussion on left shifting signed quantity within the C Programming forums, part of the General Programming Boards category; hello all, this is a quote from K&R pg no. 49 right shifting a signed quantity will fill with sign ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    left shifting signed quantity

    hello all,

    this is a quote from K&R pg no. 49
    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
    Last edited by BEN10; 03-31-2009 at 10:38 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    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 )

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Maz View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    thanks matsp.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 04:16 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21