Bit operations query on changing case

V

vindhya

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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
L

Lew Pitcher

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

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




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-----
 
C

CBFalconer

vindhya said:
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.
 
V

valli

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
 
V

vindhya

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
 
K

Keith Thompson

vindhya said:
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?)
 
O

Old Wolf

Keith said:
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.
 
K

Keith Thompson

Old Wolf said:
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.
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.
 
E

Eric Sosman

Old said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top