Python operator ">>" [duplicate] - python

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python - '>>' operator
What does the >> operator means in Python?
ie:
x = x + str(n%2)
n >> 1
Thank you

n >> 1 shifts n right 1 bit. This is the same as dividing by 2.
More generally, n >> m shifts n right m bits, giving a division by 2^m.
See also:
Shifting operations in Python's documentation
Logical shift on Wikipedia

Just for completeness, note that there's a completely different usage which is changing the stream that print uses by default:
print >> sys.stderr, message
For more information, please have a look at this related question.

That's the right shift operator.

It's the bitshift operator:
x >> n
x shifted right by n bits.

Related

What is the bit-wise NOT operator in Python? [duplicate]

This question already has answers here:
The tilde operator in Python
(9 answers)
Closed last month.
Is there a function that takes a number with binary numeral a, and does the NOT?
(For example, the function's value at 18 [binary 10010] would be 13
[binary 01101].) I thought this was what the tilde operator (~) did, but it only adds a minus sign to 18, which is two's complement of that, instead of getting 13.
As mentioned in the comments ~ is the bitwise NOT.
If you want a 5 bit unsigned bitwise NOT you can use an XOR with a mask:
>>> n = 0b10010 # 18
>>> m = 0b11111
>>> n ^ m
13

python ** vs math.pow() regarding negative values [duplicate]

This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 6 years ago.
from math import pow
assert pow(-3, 2) == 9
assert -3 ** 2 == -9
why are the two above assertions valid?
in regular math, when a negative numbered is powered to 2, it becomes positive. which one of these is equal to the regular math I know?
is ignoring negative value the only difference between these two methods?
Its because of the order in which the operations are performed. In the first case, pow(-3,2) takes as inputs a -3 as first input and a 2 as the second input. In the second case, the ** has precedence over the -, so the order in which the operations are executed is
Calculate 3**2
Change the sign of the result
This leads to the result being -9.
Because python calculate the minus after it calculate the power.
In [2]: -3**2
Out[2]: -9
In [3]: (-3)**2
Out[3]: 9

Ternary Conversion in Python not working well. Why? [duplicate]

This question already has answers here:
Convert decimal to ternary(base3) in python
(3 answers)
Closed 6 years ago.
I've tried to make a ternary calculator on Python, with some other functions like hex, bin, and oct with the built-in functions. There isn't one for ternary so I built one.
def ternary(n):
e = n/3
q = n%3
e = n/3
q = e%3
return q
i = int(input("May you please give me a number: "))
print("Binary "+bin(i))
print("Octal "+oct(i))
print("Hexadecimal "+hex(i))
print("Ternary "+ternary(i))
enter code here
But it doesn't work. Why? Where is the problem?
There are a couple of mistakes in your code which others have pointed out in the comments, but I'll reiterate them
You are using regular division when you want to use integer division (in Python 3).
You are returning an integer from your function whereas bin, oct, and hex all return strings.
In addition, your ternary function is incorrect, even with the errors fixed. The best way to write a function for base conversion on your own is with recursion.
def ternary(n):
if n == 0:
return ''
else:
e = n//3
q = n%3
return ternary(e) + str(q)

Python Bitshift 32 Bit Constraint [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Problem in calculating checksum : casting int to signed int32
This should be a relatively easy answer, I just don't really know how to search for it...I got a few semi-relevant things, but nothing that fits what I'm trying to do.
>>> 1171855803 << 7
149997542784L # I want -326312576
In other words, treat the number as an integer and don't allow it to convert to a long.
How would I do this?
I tried the solution in this question:
>>> x = 0xFFFFFFFF & (1171855803 << 7)
>>> if x > 0x7FFFFFFF: print -(~(x - 1) & 0xFFFFFFFF)
else: print x
-326312576L # yay!
It works!
May not be the best answer, but this works...
import numpy as np
np.int32(1171855803) << 7
You could try
import ctypes
a=ctypes.c_int32(1171855803)
a.value<<=7
print a
which gives: c_int(-326312576)
It seems to allow platform specific bit-manipulations. I am not sure about efficiency.

The % function in Python [duplicate]

This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 9 years ago.
I'm working on the exercises in Learning Python the Hard Way and I am wondering what the % function does. I am getting the wrong answer when i count my eggs and hoping understanding this will help me understand what I'm doing wrong.
I can't really tell why your code is broken because you haven't shown anybody what your code is. Please post samples and links next time.
Python % is used in two places, one is mathematical (the modulo operator), and the other has to do with formatting text. I'm going to assume "counting eggs" means the math way.
The modulo operator in X % Y means "Divide X by Y and give me the remainder." So:
10 % 2 == 0
10 % 3 == 1
10 % 11 == 10
That is the the modulo operator

Categories