Use of Bit-wise Arithmetic operators with Pointers
We can perform Bit-wise operation between two pointers like:
Address1 & Address2 = Illegal
Address1 | Address2 = Illegal
Address1 ^ Address2 = Illegal
~Address = Illegal
Lets explain Bit-wise operator illegal use with the help of a C++ program
a. Write a C++ program which demonstrate the use of Bit-wise OR (|) use with pointers.
#include<iostream>
using namespace std;
int main()
{
int x=10;
int y=20;
int *ptr1 = &x;
int *ptr2 = &y;
cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1|ptr2;
return 0;
}
}
The above program output is tested on www.jdoodle.com
Output:
jdoodle.cpp: In function 'int main()':
jdoodle.cpp:9:70: error: no match for 'operator|' (operand types are 'std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}' and 'int*')
cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1|ptr2;
or
compile time error
b. Write a C++ program which demonstrate the use of Bit-wise AND (&) use with pointers.
#include<iostream>
using namespace std;
int main()
{
int x=10;
int y=20;
int *ptr1 = &x;
int *ptr2 = &y;
cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1 & ptr2;
return 0;
}
}
The above program output is tested on www.jdoodle.com
Output:
jdoodle.cpp: In function 'int main()':
jdoodle.cpp:9:71: error: no match for 'operator&' (operand types are 'std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}' and 'int*')
cout<<" Value of Bit-wise OR operator use with ptr1 and ptr2 "<< ptr1 & ptr2;
or
compile time error
Thanks
Mukesh Rajput
Post A Comment:
0 comments: