# Thread: bitwise operations

1. ## bitwise operations

Code:
int main(void)
{
char line[26], newline[26];
int length, i;

printf("Enter a word: ");
scanf("%26s", &line);
length = strlen(line);

for (i=0; i<length; i++)
{
newline[i] = line[i] ^ '20';
printf("%c", newline[i]);
}
printf("\n");
}
hi,

in this code, when i input 'a' i get a 'Q'. and when i input a 'Q' i get a 'a'. the purpose of my code is to change the case; capital to lower and vice versa.

what i did was use an array and individually change the 6th bit. but the result changes the 5th and 6th bit. i exclusive or'd the 6th bit using '20'.

8421 8421
0010 0000

can someone see what i'm doing wrong?

thanks,
barneygumble742

2. Im not sure but try 0x20..

Without the quotes, might get some conversion errors though...

3. WOW...it works perfectly, no errors.

thanks.

4. Great!

The problem was the binary number 0010 0000 is 20 hex and '20' would have been the characters 2 and 0 (or maybe just 20, not sure) which is something else in hex (example: character 0 is 30 in hex).

5. Originally Posted by barneygumble742
Code:
int main(void)
{
char line[26], newline[26];
int length, i;

printf("Enter a word: ");
scanf("%26s", &line);
length = strlen(line);

for (i=0; i<length; i++)
{
newline[i] = line[i] ^ '20';
printf("%c", newline[i]);
}
printf("\n");
}
hi,

in this code, when i input 'a' i get a 'Q'. and when i input a 'Q' i get a 'a'. the purpose of my code is to change the case; capital to lower and vice versa.
Not with that code you don't.
Originally Posted by barneygumble742
can someone see what i'm doing wrong?

thanks,
barneygumble742
That will not compile. That's what's wrong with it. You can't have a multi-character character constant. That's what your compiler tells you. Pay attention to it some time:
warning: multi-character character constant
Quzah.

6. I was wondering if that would have even compiled, but it sounded like he had ran the program and got a bad result. Guess i'll learn to ask that next time

7. Well their code actually has a number of problems, in addition to the multi-character character constant issue:
Code:
char line[26], newline[26];
int length, i;

printf("Enter a word: ");
scanf("%26s", &line);
You don't use the & opperator here, since the name of an array is a pointer to its first element. (Arrays & Pointers)

Code:
int main(void)
{
...snip...
]
printf("\n");
}
It's good to declare main correctly, but the function is missing its return statement.

Furthermore, the logic is actually flawed. After fixing his errors, and switching '20' to 0x20 like you suggested, there is still this problem:

input: Hello!
output: hELLO

input: l33t
output: LT

Technicly, I suppose if all you want are letters, it would works.

Quzah.

8. Adding the 0x20 bit isn't a nice way to do it really, because on a different character set it will almost certainly fail. Assuming that 'A' to 'Z' are contiguous and that 'a' to 'z' are contiguous (not too unreasonable an assumption to make for any character set), then you can swap case using something such as this:

Code:
char c;
...
c = 'S'; /* Any arbitrary character picked up from ... well, wherever */
...
c -= ((c >= 'a' && c <= 'z') || (c >='A' && c <= 'Z')) ? 'A' - 'a' : 0;  /* Swap case if letter */
...
The subtraction does the hard work for you.

Also, the 0x20 approach is not overly clear - you'd need to comment the fact. Treat is as a coincidence that in ASCII 'a' - 'A' just happens to be 0x20.

9. > not too unreasonable an assumption to make for any character set
http://www.natural-innovations.com/c...ciiebcdic.html
EBCDIC is one such encoding used by a whole raft of IBM machinery

Stick to using toupper() etc and get on with life...

10. the program works nice. i didn't get any error messages when i used '20'. i'm using msvc++ (forgot ver #).

after using 0x20, it works perfectly when using letters. with numbers or all other characters...that's my next task. i guess i have to use a function to check wheter than enter characters are between 65 and 90 and 97 to 122. for some strange reason i'm having a ton of problem with that because i'm not good at functions/pointers. as u can tell i'm barely and amateur at C.

i did not assume that its a coincidence. asm is my natural language and i know that the only difference between caps and lower is the 6th bit. that's why i wanted to use this method rather than using a predefined function from a lib (unknown to me).

11. Stick to using toupper() etc and get on with life...
Good recommendation, but this is a great task for a beginner to try and accomplish - making a real program, but still being able to keep things quite simple.

Popular pages Recent additions