Thread: bitwise operations

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    Question 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. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Im not sure but try 0x20..

    Without the quotes, might get some conversion errors though...
    What is C++?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    WOW...it works perfectly, no errors.

    thanks.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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).
    What is C++?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Quote 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.
    Hope is the first step on the road to disappointment.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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
    What is C++?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    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. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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 subscribe to a feed

Similar Threads

  1. Bitwise Operations to Read ON/OFF Bits
    By pseudonoma in forum C Programming
    Replies: 4
    Last Post: 02-25-2008, 03:15 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. Bitwise operations
    By sh3rpa in forum C++ Programming
    Replies: 16
    Last Post: 09-25-2007, 06:32 PM
  4. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 04:48 AM
  5. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-28-2002, 12:46 AM