Bitwise (need help not answer)

This is a discussion on Bitwise (need help not answer) within the C Programming forums, part of the General Programming Boards category; I'm studying bit manipulation. My assignment is to write two functions. 1. convert string to all upper, 2. convert string ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    Bitwise (need help not answer)

    I'm studying bit manipulation. My assignment is to write two functions. 1. convert string to all upper, 2. convert string to all lower. The trick is that I cannot use toupper or tolower. I have to use bit manipulation. I have read many articles and the chapters of various books on bitwise. It's very hard to find real world examples showing bit manipulation in C. Or at least I just don't know how to look for them.

    What I know (or think I know)...
    I understand that the easiest way to manipulate bits in C is to use hex. I have read and understand the basics of binary and hexadecimal. I understand that the difference between an uppercase character and a lowercase character is the 3rd lower order bit (I think). I understand how to create a mask to turn on and off this bit. But I just don't get how to get a char (getchar?), and manipulate a specific bit of that character using the mask.

    Can anyone suggest a tutorial or provide any insight?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Any tutorial that covers bitwise should be good. Remember, everything stored in the computer is already in bits, so you don't do anything to a character (or whatever) to get it ready.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,009
    To get a character from a string is just str[i], where str is the string, and i is the index.

    To manipulate a specific bit use:
    Code:
    chtr |= MASK; /*sets all bits that are set in MASK*/
    chtr &= MASK; /* resets bits not set in MASK*/
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Look in stdio.h - you'll find all the input/output functions.

    Specifically, getc will give you a single char, or you could work on a string and access a specific char ( string[0] ), or better yet, get a string and use a loop to perform the operation on every char inside it!

  5. #5
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    upper>>lower

    Upper =>Lower

    u can use bitwise OR for characters in string .. like char1 =char1 |32

  6. #6
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    You all are awesome! Here is what I have so far... Not working yet but not sure if that's because of other problems in my program...
    Code:
    char* b_returnLowerCase(char originalString[], char lowerString[])
    {
    	int i;
    	int length = strlen(originalString);
    
    	for (i = 0; i < length; ++i)
    	{
    		lowerString[i] = originalString[i] | MASK;
    	}
    	lowerString[i] = '\0';
    	puts(lowerString);
    
    	return lowerString;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  3. Tic Tac Toe program...
    By Kross7 in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 03:25 PM
  4. code help :)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-28-2002, 12:12 PM
  5. Replies: 22
    Last Post: 11-08-2001, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21