如何在C ++中将char和int与按位运算符进行比较? [英] How can I compare char and int with bitwise operators in C++ ?

查看:178
本文介绍了如何在C ++中将char和int与按位运算符进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c ++中将char和int与按位运算符进行比较?我无法理解当我输入A时,输出会很小a。为什么?实际上,int number 32和character的比较是什么意思?我怎么能达到那个结果呢?如果有人知道这个,请让我知道:)另一个问题,是否可能使用&或^(独家或)比较它们?

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?

#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;
}





我的尝试:



我搜索了这个,但我找不到与bitwise运算符相关的答案。



What I have tried:

I have searched about this,but i can't find answer which is related bitwise operator.

推荐答案

"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.


你的代码没有比较任何东西:它从用户读取一个字符,确保一个特定的位是一个 - 从值32你是二进制ORing字符 - 由于十进制值32是十六进制20,A和a之间的差值是一位,所以输出它。

http://www.asciitable.com/index/asciifull.gif [ ^ ] OR将确保输出的字符始终为小写。除非它在这个集合中的字符:@ [\] ^转换为`{|}〜和_,这将取决于字体。



和不,& (AND)和^(XOR)也不进行比较。要比较任何东西,你需要使用条件指令,如,如果,而,例如。
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.


引用:

我无法理解当我输入A时,输出会很小a。为什么?

I can't understand that when i enter A,output will be small a.Why?

使用调试器查看代码中发生的情况。



您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />


对于你的实际问题:

A 是ASCII码65而 a 是ASCII码97:差值是32 。

ASCII - 维基百科,免费的百科全书 [ ^ ]

教程 - C和C ++中的按位运算符和位操作 - Cprogramming.com [ ^ ]



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[^]

引用:

另一个问题,是否有可能使用&或^(独家或)比较它们?

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]

引用:

但如果我比较char和int,必须我将其中一个更改为另一个类型?比如,如果我比较x大于4,我必须将x更改为int,或者将4更改为char吗?

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?

看起来您需要正确学习C / C ++

以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

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 ++编程语言 [ ^ ]

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[^]


这篇关于如何在C ++中将char和int与按位运算符进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆