473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Bit operations query on changing case

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

Nov 15 '05 #1
9 1667
-----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)

iD8DBQFC726DagVFX4UWr64RAqoYAKDXoCVzPuoH105hUyUV1e cHrYi39wCgvRnT
qYKLJ4sfd3mVcjw4+u2w8qs=
=E4um
-----END PGP SIGNATURE-----
Nov 15 '05 #2
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
Nov 15 '05 #3
-----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/agVFX4UWr64RAiMNAJ4wIuvAQRN5cJCoJrn7dWadq8LXVwCeNF eB
kw2HxtpqKk3TM43OuhUC4LE=
=/ex2
-----END PGP SIGNATURE-----
Nov 15 '05 #4
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


Nov 15 '05 #5
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

Nov 15 '05 #6
"vindhya" <ma*********@gmail.com> 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) ks***@mib.org <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.
Nov 15 '05 #7
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.

Nov 15 '05 #8
"Old Wolf" <ol*****@inspire.net.nz> 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) ks***@mib.org <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.
Nov 15 '05 #9
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
es*****@acm-dot-org.invalid

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
6
by: Ajay | last post by:
Hi I am trying to do the following. I have a set of records Example 1 Test 2 test 3 TEST 4 ACCESS 5 EXCEL
2
by: ILCSP | last post by:
Hello, I'm in the process of changing our 'normal' Access 2000 update queries to Update Pass Through Queries. We have a SQL server 2000 database and we're using an Access 2000 database as our...
4
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
3
by: bill | last post by:
All, I have not visited Access for a while, and I am drawing a blank on how to search/sort a column of data (integer) for/on a particular bit pattern. Actually, a SQL example would be great. ...
23
by: Anders Borum | last post by:
Hi! I am implementing a threaded producer / consumer pattern just for fun. I am using an internal counter to keep track of the produced / consumed items and am logging that information. I am...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.