DarkKnight69

Golden Member
Jun 15, 2005
1,688
0
76
I need some help with bitwise operations. I apparantly did not learn them last semester and the teacher has now decreed unless i learn them fast I am ******!!

Disclaimer: THIS IS NOT A HOMEWORK ASSIGNEMENT, IT IS AN ICA THAT I HAVE ALREADY FAILED!!!

Ok.

So here are the instructions:

1. Declare 2 arrays of char, both of length 256.

2. Prompt the user to enter some text, then accept the text into one of the arrays of char. Use and operation from the cstring header to copy the input string into the second array of char.

3. Proceeding character by character up to BUT NOT INCLUDING null, perform the following bitwise operation on each character in the second array of char:
a)Use an AND mask to clear (make equal to 0) bits 6 and 7.
b)Use an OR mask to set (make equal to 1) bits 0 and 1.
c)Perform a left shift of one bit position.

4. Display both the original string and the modified string, with suitable documentation.

i have the basic shell for the program, but i dont know how to write the bitwise operations to dop as requested. I know they will belong in the for loop.





#include <iostream>
#include <cstring>
using namespace std;

int main(int argc, char**argv)
{
char line[256];
char modify[256];

cout << "Please Enter a short message: " << endl;
cin.getline(line,256);
cout << endl;

strcpy(modify, line);

for(int n=0; modify[n]!=0; n++)
{



}

cout << "The original string Read: " << line << endl;
cout << "The scarambled string Reads: " << modify << endl;

return 0;
}

I woyuld appreciate an explaination as to what i need to do to get this operation to work.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Have you read the friendly manual on the bitwise and shift operators?

Take the bitwise AND operator, do you know what it is? given an 8-bit char do you know how to modify it with AND?

You need to learn these, not just copy code. (Not saying you aren't trying, but your OP doesn't ask any specific questions or show you've tried on your own)
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Suppose you have a binary number: 10101010 (bits are numbered 0-7 from right to left).

To clear the left two bits and preserve the rest as-is, you'd want to do this:
10101010 &
00111111
========
00101010

Look at what's happening - anywhere you have a 1 and a 0 or two 0s, you end up with a 0. Anywhere with two 1s you end up with a 1. So to use an & mask to clear bits, you need that & mask to have 0s in those bits and 1s everywhere else.

Now think about how you'd use an or (|) to set bits.

& and | work the same way as && and ||, but they do it for each bit individually.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Spend 30 mintues with the manual reading on the string operators.
Then another 30 with regards to bitwise operations.

That way you will be able to actually do what the teacher requires rather than faking it again.
 

DarkKnight69

Golden Member
Jun 15, 2005
1,688
0
76
umm, i know what is requred, I dont not understand how to make the program do it.i know the AND and OR operations and how they compare in Binary, I even understand that in order to make bits 6 and 7 equal zero and i know how to make bits 1 and 2 equal 1. I do not know, however, how to make the program do it!

So my question is how do i use these operators to compare them.

i cannot simply go

line & modify;

I cannot figure out how I do the compare!
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
variable &= 0x00000000 should work. Of course, not 0x00000000, but whatever you figured out. Same thing as variable = variable & 0x00000000.
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
modify[n] = modify[n] & 63;

63 is that bit mask in decimal.

Now can you apply that knowledge to the second part?
 

DarkKnight69

Golden Member
Jun 15, 2005
1,688
0
76
Originally posted by: xtknight
variable &= 0x00000000 should work. Of course, not 0x00000000, but whatever you figured out. Same thing as variable = variable & 0x00000000.

Thanks for the help mugs, this is actually all i needed.

I knew my masks already i just didnt know the syntax to apply them. I didnt thisk it was so simple as that, I was unsure whether i was missing a variable or something!
 

DarkKnight69

Golden Member
Jun 15, 2005
1,688
0
76
for(int n=0; modify[n]!=0; n++)
{
modify[n]= modify[n] & 63;
modify[n]= modify[n] | 3;
modify[n]= modify[n] << 1;
}

thanks for the help to those who gave it, I was not looking for the answer, I was looking for a push in the right direction. I appreciate all who were kind enough not to be cynical ******!
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Good job. Now do you understand WHY it works? modify[n] & 63 has a value, but it doesn't DO anything with it. You could use that in a conditional if you wanted to and not modify the data in any way. You have to store it back in itself to make the change.

They're not being cynical, they know that if you don't understand how it works you're not helping yourself. Bitwise operators were glazed over in my classes in college, but I use them every day in my work. You might have to pick up someone else's code and figure out how it works, and you won't be able to do that if they used bitwise operators. :)
 

DarkKnight69

Golden Member
Jun 15, 2005
1,688
0
76
No, i didnt relize it was so simple. I was thinking I had to make a variable or another string or something of that sort, not that I could just compare it to decimal numbers. If I knew that I would have been golden.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Still, if you had read what the "bitwise" operators are in your book or compiler docs, you could have answered this on your own.

Learning to look up functions, operators, precedence in the friendly manual is an important skill you do need to learn.

Also, you are doing "assignment" not "comparison" to solve this problem.

if (x > y) // comparison

a += 5 ; // assignment, shorthand for a = (a + 5) ;

x |= 0x0f ; // assignment of result of bitwise arithmetic (not logical comparison)

I'm not trying to give you a hard time, these are things you need to know and learn to be successful.