# Thread: Bitwise (need help not answer)

1. ## 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. 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. 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*/```

4. 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. ## upper>>lower

Upper =>Lower

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

6. 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