Bit operations query on changing case

Discussion in 'C Programming' started by vindhya, Aug 2, 2005.

  1. vindhya

    vindhya Guest

    I have a small query.
    If I have a character from alphabets and I want to change case of the
    same then how is it possible ?
    Here are the solutions, but can you suggest any other solution may be
    using bit operations.

    Solution#1 : Use tolower/toupper
    Solution#2 : Add/Subtract 32

    Thanks in advance
    ms
    vindhya, Aug 2, 2005
    #1
    1. Advertising

  2. vindhya

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    vindhya wrote:
    > I have a small query.
    > If I have a character from alphabets and I want to change case of the
    > same then how is it possible ?
    > Here are the solutions, but can you suggest any other solution may be
    > using bit operations.
    >
    > Solution#1 : Use tolower/toupper


    Best solution, as it accomodates (or /should/ accomodate) any usable
    target characterset.

    > Solution#2 : Add/Subtract 32


    Good only for ASCII.


    Solution #3: Bit set/clear bit 2 (3rd bit from left, in 8 bit character)
    3a) using logical operations ( (target & 040) and (target | 040) )
    3b) using a structure with bitfields

    Good only for ASCII.


    - --

    Lew Pitcher, IT Specialist, Enterprise Data Systems
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFC726DagVFX4UWr64RAqoYAKDXoCVzPuoH105hUyUV1ecHrYi39wCgvRnT
    qYKLJ4sfd3mVcjw4+u2w8qs=
    =E4um
    -----END PGP SIGNATURE-----
    Lew Pitcher, Aug 2, 2005
    #2
    1. Advertising

  3. vindhya

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Lew Pitcher wrote:
    > vindhya wrote:
    >
    >>>I have a small query.
    >>>If I have a character from alphabets and I want to change case of the
    >>>same then how is it possible ?
    >>>Here are the solutions, but can you suggest any other solution may be
    >>>using bit operations.
    >>>
    >>>Solution#1 : Use tolower/toupper

    >
    >
    > Best solution, as it accomodates (or /should/ accomodate) any usable
    > target characterset.
    >
    >
    >>>Solution#2 : Add/Subtract 32

    >
    >
    > Good only for ASCII.
    >
    >
    > Solution #3: Bit set/clear bit 2 (3rd bit from left, in 8 bit character)
    > 3a) using logical operations ( (target & 040) and (target | 040) )

    Gak! I gotta learn to finish my coffee first. It wakes me up enough to
    see my errors...

    Make that

    3a) using logical operations ( (target & ~040) and (target | 040) )


    > 3b) using a structure with bitfields
    >
    > Good only for ASCII.
    >
    >
    > --
    >
    > Lew Pitcher, IT Specialist, Enterprise Data Systems
    > Enterprise Technology Solutions, TD Bank Financial Group
    >
    > (Opinions expressed here are my own, not my employer's)


    - --

    Lew Pitcher, IT Specialist, Enterprise Data Systems
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFC73F/agVFX4UWr64RAiMNAJ4wIuvAQRN5cJCoJrn7dWadq8LXVwCeNFeB
    kw2HxtpqKk3TM43OuhUC4LE=
    =/ex2
    -----END PGP SIGNATURE-----
    Lew Pitcher, Aug 2, 2005
    #3
  4. vindhya

    CBFalconer Guest

    vindhya wrote:
    >
    > I have a small query.
    > If I have a character from alphabets and I want to change case of
    > the same then how is it possible ?
    > Here are the solutions, but can you suggest any other solution
    > may be using bit operations.
    >
    > Solution#1 : Use tolower/toupper
    > Solution#2 : Add/Subtract 32


    Only solution 1 is portable.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Aug 2, 2005
    #4
  5. vindhya

    valli Guest

    Hi vindhya,
    I am giving my solution.For 'A' ASCII is 65(41H) and
    for 'a' ASCII is 97(61H) so for lower case conversion OR with 20H and
    for upper case conversion EOR with 20H.
    bye
    regards
    valli
    vindhya wrote:
    > I have a small query.
    > If I have a character from alphabets and I want to change case of the
    > same then how is it possible ?
    > Here are the solutions, but can you suggest any other solution may be
    > using bit operations.
    >
    > Solution#1 : Use tolower/toupper
    > Solution#2 : Add/Subtract 32
    >
    > Thanks in advance
    > ms
    valli, Aug 3, 2005
    #5
  6. vindhya

    vindhya Guest

    Thanks a lot guys.
    The solution proposed above works. What I find that whatever be the
    approach the solution will always be on ASCII difference between the
    integral value of upper and lower case.

    MS
    vindhya, Aug 3, 2005
    #6
  7. "vindhya" <> writes:
    > Thanks a lot guys.
    > The solution proposed above works. What I find that whatever be the
    > approach the solution will always be on ASCII difference between the
    > integral value of upper and lower case.


    Above? The only thing I see "above" is the article headers.

    Perhaps you can offer us some advice. We've been trying repeatedly to
    teach people how to post properly, but it just doesn't work. The
    groups.google.com interface is broken, but it's still possible to use
    it correctly. As we've posted here hundreds of times:

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    And yet we *still* see people posting through groups.google.com with
    no attributions and no context, apparently assuming (incorrectly) that
    everyone who reads the followup can see the parent article.

    Other than getting Google to fix their interface (we've tried that
    too), how can we spread the word? How can we make people understand
    that blindly posting with no context is disrupting this newsgroup?
    What would have worked for you?

    (And as for your original question, just use toupper() and tolower().
    Changing case by bit manipulation is error-prone, non-portable, and
    completely unnecessary. Why do you think toupper() and tolower() were
    written in the first place?)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 3, 2005
    #7
  8. vindhya

    Old Wolf Guest

    Keith Thompson wrote:
    >
    > Perhaps you can offer us some advice. We've been trying repeatedly to
    > teach people how to post properly, but it just doesn't work. The
    > groups.google.com interface is broken, but it's still possible to use
    > it correctly. As we've posted here hundreds of times:
    >
    > Other than getting Google to fix their interface (we've tried that
    > too), how can we spread the word? How can we make people understand
    > that blindly posting with no context is disrupting this newsgroup?
    > What would have worked for you?


    How would Google fix their interface? Even if they provide
    auto-quoting, then we will just have legions of top-posters,
    which is IMHO worse than non-quoting.

    > (And as for your original question, just use toupper() and tolower().
    > Changing case by bit manipulation is error-prone, non-portable, and
    > completely unnecessary. Why do you think toupper() and tolower() were
    > written in the first place?)


    If the code is for a freestanding implementation, which doesn't
    have to support the ctype functions, then adding or subtracting
    ('a' - 'A') might be advisable.
    Old Wolf, Aug 3, 2005
    #8
  9. "Old Wolf" <> writes:
    > Keith Thompson wrote:
    >>
    >> Perhaps you can offer us some advice. We've been trying repeatedly to
    >> teach people how to post properly, but it just doesn't work. The
    >> groups.google.com interface is broken, but it's still possible to use
    >> it correctly. As we've posted here hundreds of times:
    >>
    >> Other than getting Google to fix their interface (we've tried that
    >> too), how can we spread the word? How can we make people understand
    >> that blindly posting with no context is disrupting this newsgroup?
    >> What would have worked for you?

    >
    > How would Google fix their interface? Even if they provide
    > auto-quoting, then we will just have legions of top-posters,
    > which is IMHO worse than non-quoting.


    They could open the input window with the cursor at the bottom. They
    could put up a brief message telling people how to post properly.

    Some posters do use groups.google.com properly. I haven't noticed
    that they top-post more often than average.

    >> (And as for your original question, just use toupper() and tolower().
    >> Changing case by bit manipulation is error-prone, non-portable, and
    >> completely unnecessary. Why do you think toupper() and tolower() were
    >> written in the first place?)

    >
    > If the code is for a freestanding implementation, which doesn't
    > have to support the ctype functions, then adding or subtracting
    > ('a' - 'A') might be advisable.


    Sure, but I doubt that the OP is using a freestanding implementation.

    There are still a few things I'd want to consider before resorting to
    the 'a'-'A' trick. Can I be sure that the code will only be used on
    systems that use ASCII or something like it? (I've seen programmable
    calculators that can display letters but don't use ASCII codes.) Do I
    need to worry about letters other than 'A'..'Z' and 'a'..'z' (even
    embedded systems might need to deal with accented letters).

    I might write my own toupper() and tolower() macros, to be defined
    only if <ctype.h> doesn't exist (configured by the build process), so
    I can just change the definition in one place when my assumptions turn
    out to have been incorrect.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 3, 2005
    #9
  10. vindhya

    Eric Sosman Guest

    Old Wolf wrote:
    >
    > If the code is for a freestanding implementation, which doesn't
    > have to support the ctype functions, then adding or subtracting
    > ('a' - 'A') might be advisable.


    #define TOUPPER(x) ((x) + ('A' - 'a'))
    putchar (TOUPPER('B'));
    putchar (TOUPPER('#'));
    putchar (TOUPPER('\n'));
    ...

    Some pre-Standard implementations of toupper() and tolower()
    actually worked this way, meaning that they botched any argument
    that wasn't a letter of the expected case. That's why you found
    (and may still find) sequences like

    if (islower(ch))
    ch = toupper(ch);

    .... which leads one to ponder just how "efficient" the simplistic
    implementation was, anyhow.

    --
    Eric Sosman
    lid
    Eric Sosman, Aug 4, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    3,999
    robert
    Feb 11, 2006
  2. Replies:
    3
    Views:
    1,714
    Timothy Bendfelt
    Jan 19, 2007
  3. Replies:
    9
    Views:
    938
    Juha Nieminen
    Aug 22, 2007
  4. Replies:
    8
    Views:
    366
  5. Jeff.M
    Replies:
    6
    Views:
    162
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page