Hi,
If got a C++ assignment for school, but im stuck :(
This is assignment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
using namespace std;
const short bit0 = 0;
const short bit1 = 1;
const short bit2 = 2;
const short bit3 = 3;
const short bit4 = 4;
const short bit5 = 5;
const short bit6 = 6;
const short bit7 = 7;
int main(){
unsigned char p1 = 0xA0;
unsigned char p2 = 0x3F;
cout << "p1 en p2 have the value:" << endl;
// set output to hex mode
cout << (short)p1 << '\t' << (short)p2 << endl;
//setting of bit 2 in a byte:
p1 = p1 | (1<<bit2);
//resetting of bit 4 in a byte:
p2 = p2 & ~(1<<bit4);
cout<<"p1 en p2 have after the editing the value:"<< endl;
cout << (short)p1 << '\t' << (short)p2 << endl;
system("PAUSE");
return 0;
}
|
and we need to do this:
Set bit2 of p1, and reset bit7
Please help me :(
Greetings
Last edited on
could you translate a bit, it would make it a bit clearer
@John2,
Could you explain it to me? i dont get anything of the assignment :(
<< is a bitwise operator. It shifts the bit in a byte to the left, compared to >> which shifts them to the right:
1<<bit2 = 1<< 2 ofcourse cause bit2 is declared to be 2.
1 = 00000001 in byte form.
Now the operation:
00000001 <<
00000100 You can see it 'shifts' the bit to the left two places.
Now for: p1 = p1 | (1<<bit2);
p1 = 0xA0 = 160 = 10100000
10100000
00000100 | (the or operator)
10100100 = 164
Now for: p2 = p2 & ~(1<<bit4);
p2 = 0x3F = 63 = 00111111
1<<bit4 = 1<<4 = 00010000
00010000 ~ (bitwise not operator)
11101111
00111111
11101111 & (bitwise and operator)
00101111 = 47