Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i compare char and int with bitwise operators in c++ ?I can't understand that when i enter A,output will be small a.Why?Actually,what does "comparing of int number 32 and character" mean?how can i reach that result?If someone know this,please let me know also :) Another question ,is it possible use & or ^(exclusive or) to compare them?
C++
#include<iostream>
using namespace std;
int main()
{
	int t=32;
	char ch;
	cout<<"Enter a capital letter:";
	cin>>ch;
	ch=(ch|t);
	cout<<ch<<endl;
	return 0;
}


What I have tried:

I have searched about this,but i can't find answer which is related bitwise operator.
Posted
Updated 11-Sep-16 7:10am

"A" is hex 0x41
32 is hex 0x20
0x41 | 0x20 is 0x61 which is "a"


I have no idea what you are trying to achieve, perhaps you should edit your question.
 
Share this answer
 
Comments
Member 12702056 11-Sep-16 14:26pm    
Thank you,i got what i tried to understand :)
Your code doesn't "compare" anything: it read a character from the user, makes sure that a specific bit is to one - from the value 32 you are Binary ORing the character with - and outputting it.
Since the decimal value 32 is hex 20, and teh difference between "A" and "a" is one bit: http://www.asciitable.com/index/asciifull.gif[^] the OR will ensure that the character it output is always lower case. Unless it's character in this set: "@[\]^" which get converted to "`{|}~" and "_" which will be font dependant.

And no, & (AND) and ^ (XOR) do not do comparisons either. To compare anything, you need to use a conditional instruction like if or while for example.
 
Share this answer
 
Comments
Member 12702056 11-Sep-16 14:25pm    
Thanks for your answer,but actually you explained in the difficult way for me :)
Especially this part:"the OR will ensure that the character it output is always lower case. Unless it's character in this set: "@[\]^" which get converted to "`{|}~" and "_" which will be font dependant."
Quote:
I can't understand that when i enter A,output will be small a.Why?
Use the debugger to see what happen in your code.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.

For your actual problem:
A is ASCII code 65 and a is ASCII code 97: difference is 32.
ASCII - Wikipedia, the free encyclopedia[^]
Tutorials - Bitwise Operators and Bit Manipulations in C and C++ - Cprogramming.com[^]

Quote:
Another question ,is it possible use & or ^(exclusive or) to compare them?
It is not the general usage, but yes, it is possible, but everything depend on what kind of comparison you want to do.

[Update]
Quote:
But if i compare char and int ,must i change one of them to another one type?like,if i compare x is greater or 4 ,must i change x to int,or 4 to char?
Looks like you need to learn C/C++ properly
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
v4
Comments
Member 12702056 11-Sep-16 13:13pm    
Thank you for your answer :) Actually,i have tried to learn debugging,but i can't find good explanations for debugging in dev c++,and i have tried in Visual Studio also,but i can't download Visual Studio without problems,all of times it causes problems and errors in my computer.
Member 12702056 11-Sep-16 13:34pm    
I meant comparison like this one,comparison between number and character,can we use and ,exclusive OR in programs like this?does that mean anything?
Patrice T 11-Sep-16 13:43pm    
In this case it don't mean much.
Use normal comparison operators.
Member 12702056 11-Sep-16 14:24pm    
But if i compare char and int ,must i change one of them to another one type?like,if i compare x is greater or 4 ,must i change x to int,or 4 to char?
Patrice T 11-Sep-16 16:39pm    
C++ will do changes needed automatically.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900