How ~12 gives -13 in decimal? [duplicate] - python

This question already has answers here:
What is “two's complement”?
(24 answers)
How to think about Python's negative number bitwise operations?
(2 answers)
Closed 1 year ago.
I have a question in my programming class, it is: What is the value of ~12 and the answer is -13. I don't understand why? I convert 13 base 10 to binary, which is 1100, than I switched all the 1 for 0 and vice versa, it gives me 0011, so I thought the answer was 3 but it's not.

Converting 12 to a signed binary number yields 01100 then assuming that ~ is supposed to invert every bit we get 10011 which can be converted from signed binary to decimal yielding -13.

Related

Python expresses a number as a really large integer rather than a negative number [duplicate]

This question already has answers here:
Converting bitstring to 32-bit signed integer yields wrong result
(3 answers)
Closed 2 years ago.
Is there a way to force python to treat a number based upon its sign?
E.g. 0xFFFFFFFF = -1 instead of 4294967295?
You can use ctypes.c_int32 for a signed 32 bit integer:
import ctypes
wrapped = ctypes.c_int32(0xFFFFFFFF)
print(wrapped) # c_int(-1)
print(wrapped.value) # -1

how to do division in python without decimal points [duplicate]

This question already has an answer here:
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
if I use integer type cast conversion technique then it doesn't work for large numbers like 12630717197566440063
I got wrong answer in some cases like below in python 3
a =12630717197566440063;
print(a)
temp = a/10
print(int(temp))
Then I am getting 1263071719756644096 as a answer instead of 1263071719756644006
You can use the // (floor division) operator:
temp = a//10
print(temp)

How to round float in multiple of 5 [duplicate]

This question already has answers here:
Round to 5 (or other number) in Python
(21 answers)
How do I round to the nearest 0.5?
(10 answers)
Closed 2 years ago.
I got this number 1.12412 and I want to round it to 1.12415 or 1.12410 (muliple of 5 in last decimal)
If using the Round(X,4) function I get 1.1241 (4 decimals).
Is there a function that can make that happen?
Thanks!
There is an answer in stack but using c# not python
My way to do that is to specify rounding unit first and then simple trick as below:
import numpy as np
rounding_unit = 0.00005
np.round(1.12412/rounding_unit) * rounding_unit
You may:
Multiply your number by 2
Use Round(X,4)
Divide the result by 2
profit!!!

What does "e" in "1e-5" in Python language mean and what is the name of this notation? [duplicate]

This question already has answers here:
What does the suffix e+number mean in python at the end of a float?
(2 answers)
What is the meaning of number 1e5?
(5 answers)
Closed 6 years ago.
I notice that there is such an expression "1e-5" in Python(probably in other languages also)
What is the name of this notation?
what does it denote in math?
What does 'e' mean? It's the first time I see a character helps to denote some value, are there other characters also help do so?
Why should use this way instead of some other python math operation like pow() etc.
It is scientific notation. It means 1 × 10−5. In other words, 0.00001.
10 ** -5, i.e. 10 to the power of negative 5, 1 divided by 10 to the power of 5, or 0.00001.
It means 10 to the power of -5 times 1

How python calculate this division? [duplicate]

This question already has answers here:
Negative integer division surprising result
(5 answers)
Closed 7 years ago.
How python calculate this division?
>>>-3/10
-1
Looks like python rounds the answer to the lower value.
>>> -3/4
-1
>>> -3/4.
-0.75
>>> -3/10.
-0.3
>>> -3/10
-1
This is just my guess.
Python 2, like many languages, uses integer division. Dividing two integers returns a integer (the nearest integer to the answer rounded down.)
To get a floating point result, you need to force one or more of the terms to be a float.
float(-3)/10

Categories