0
how to use operators ( ^ , & , | ) And which problems?
What are the benefits of them in math programming problems
11 Antworten
+ 4
[ how the result is calculated?
5^3 = 6
And the others]
^ is for xor
you have to think of numbers as binary. that's why they are called bitwise operators.
in xor if you get the same digit you get 0 example.
0 ^ 0 = 0
1 ^ 1 = 0
0 ^ 1 = 1
1 ^ 0 = 1
in your example
read them vertically
5 = 00000101
xor ^
3 = 00000011
=
= 00000110
00000110 binary = 6 decimal
+ 3
In this assignments based on Ascii values and Bitwise visualization. So, you should firstly think about of them. After that, the operators using properties etc. Then, you can understand how they work actually.
Also, check Bahha🐧 's comment. I think it's more clear.
Happy coding!
+ 1
I think you mean in R.
^ : used for power of any number. (exponentiation)
For example: 2^3 = 8
& : (and operator, it can used also as && in general case and it is generally used in conditional statements) also it's used for an address of a variable, like in C, C++.
For example:
x = 0, y = 1
x & y = 0
| : (or operator, it can be used also as || in general case) used in a conditional statement generally.
For example:
x = 0, y = 0
x | y = 1
+ 1
You're welcome! Mahmoud ayman
Happy coding!
https://code.sololearn.com/cqI8ecK0sEOE/?ref=app
+ 1
in python,
they are used to perform bitwise operations.
there are many scenarios where you can use them.
cryptography for example.
here is a silly example to convert string of 0-9 to int.
x = "2" #x is a string
the ASCII number for "2" is 50
50 in binary = 0011 0010
#ord() is used to get ascii number of "2"
x = ord(x) & 0b00001111
print(x) # x is now an int by removing higher half of 00110010 to get 0010 which 2 in decimal
#visual representation, compare vertically
0000 1111
&
0011 0010
=
0000 0010
= 2 in decimal.
0
mesarthim how the result is calculated?
5^3 = 6
And the others
0
You can use this and operator for this scenario consider if user gives some string you need to check whether it contains a alphabet and length should be greater than 7.Here comes and operator you can check if len(string) > 7 and string.isalpha():
pass
0
And next ^ not operator it can be used for evaluating Boolean value which gives opposite to that of your input.consider if you have variable stop which equals true you have two parts of which executes when stop variable is not true and a else part.so you can easily relate variable name and it's purpose and so you must use not operator if part executes when not stop that is if false it satisfies and viz
0
And next or operator you can use it when u need to trigger a task when either one of the conditions are true.you can club all these operator for condition ✅ check.you can solve problems related to this you can gain better knowledge for coding problems visit codewars or codingame website