Example of binary operators in C++? [duplicate] - c++

This question already has answers here:
Real world use cases of bitwise operators [closed]
(41 answers)
Closed 7 years ago.
I am currently a beginner in C++ and learning about operators.
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
unsigned int a=195;
unsigned int b=87;
unsigned int c;
c=a&b;
cout << c;
}
The Output of the above program is : 67
This is explanation
But What is the practical use of this?

This is not a boolean operator. It is a binary AND operator. The practical use of this comes from Boolean algebra, which should be studied before any programming attempt is made.

Related

Is directly putting in binary possible C++ [duplicate]

This question already has answers here:
Can I use a binary literal in C or C++?
(24 answers)
Closed 9 months ago.
Is putting in binary as a value possible? I want something like char test = 00101011 and it will become 43. I know this is possible by making a function that converts binary to decimal (which can be inputted) but thats not direct and Im pretty sure it takes time.
You need to put the prefix 0b.
#include <iostream>
int main()
{
char c = 0b00101011;
std::cout << static_cast<int>(c) << std::endl;
}

Why sizeof() method is giving different results? [duplicate]

This question already has answers here:
Size of character ('a') in C/C++
(4 answers)
Closed 2 years ago.
When I run the program below in C, I get the output result to be 4.
#include <stdio.h>
//using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
But when I run the code below in C++, I get the output result to be 1.
#include <iostream>
using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
Could you please explain why do I get different output for the same code as if 'a' is the way we define characters in both the languages ?
In C, a character representation (like 'a') has type int. So, sizeof operator returns the size of an integer.
In C++, it's of a character type.

Why two different outputs? [duplicate]

This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 5 years ago.
I am novice in programming. I have written a program and confused in concepts of pointers.
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c[]="hello";
char *a=c;
cout<<a<<endl;
int arr[]={1,2,3,5};
int *p=arr;
cout<<p<<endl;
return 0;
}
When I print a, it prints hello but when I print p it print the address. Why?
std::ostream has overload for const char* to display C-string.
int* would use the void* one which print the address.

program in c++. size of char array is 5 but value more than 5 char is also accepted [duplicate]

This question already has answers here:
Why does this work? Using cin to read to a char array smaller than given input
(2 answers)
Closed 7 years ago.
this is a short program in c++
#include<iostream>
using namespace std;
int main(){
char s[5];
cout<<"enter your name"<<endl;
cin>>s;
cout<<"Hello"<<s<<endl;
return 0;
}
value greater than 5 are also accepted
what is the concept behind it??
You're indeed correct. To attempt to put any more than 5 characters (including a null-termimator) into s is undefined behaviour.
In C++, the best thing to do is to use a std::string instead. Appropriate overloads for << and >> are provided for std::string.

How many parameters can be passed to main() [duplicate]

This question already has answers here:
Can the arguments of main's signature in C++ have the unsigned and const qualifiers? [duplicate]
(7 answers)
Closed 8 years ago.
Is there a limit for the number of parameters main can have?
Here is a sample code which runs perfectly … and I am not aware if it is allowed.
int main( char* argv[], int argc, int arv, bool test)
{
cout<<"Hello"<<endl;
}
Output:
Hello
I am using
gcc-4.1.2_20070115-0.32.53
gcc-c++-4.1.2_20070115-0.32.53
libgcc-4.1.2_20070115-0.32.53
gcc-objc-4.1.2_20070115-0.32.53
The C++ standard does not explicitly forbid these signatures, but it does not require them to work either. All it says is that the two following signatures must work on any compiler:
int main()
int main(int, char**)
And that the return type must be int.