This question already has answers here:
Two's Complement in Python
(20 answers)
Closed 6 years ago.
My code has to read 5-bit signed binary values. Let's say it reads a 11111, which would be two's complement -1. But int("0b11111", 2) returns 31 instead.
How can I parse the correct value?
Here's a possible solution testing all 5-length binary numbers of your future emulator:
import itertools
def two_complement(value, length):
if (value & (1 << (length - 1))) != 0:
value = value - (1 << length)
return value
opcodes_emulator = ["".join(seq) for seq in itertools.product("01", repeat=5)]
for my_string in opcodes_emulator:
print my_string, two_complement(int(my_string, 2), len(my_string))
Related
This question already has answers here:
modulo of a number - python vs c#
(3 answers)
Mod of negative number is melting my brain
(15 answers)
Closed 7 months ago.
I'm trying to get % result of ( -1%26 )
the result should be 25
in python it's right
but C# gives -1 as result
why and how to get 25 in C# not -1
You can use a different kind of modulus function, like this:
int Modulus(int a, int b) {
return ((a % b) + b) % b;
}
Console.WriteLine(Modulus(-1, 26)); // prints 25
This question already has answers here:
Formatting long numbers as strings
(13 answers)
Closed 9 months ago.
Converts a large integer (or a string representation of an integer) to a friendly text representation like 1000000 to 1M or 1000000000 to 1B so it can show that way on the graph, I am pretty much new to data science, I look everywhere for possible way to do it I can't, i am sure the answer will help someone else too. Thanks!
def friendly_text(i):
if i >= 1000000000000:
return str(i / 1000000000000) + 'T'
if i >= 1000000000:
return str(i / 1000000000) + 'B'
if i >= 1000000:
return str(i / 1000000) + 'M'
if i >= 1000:
return str(i / 1000) + 'K'
print(friendly_text(2555))
print(friendly_text(241555))
print(friendly_text(241535555))
print(friendly_text(2415533347615))
print(friendly_text(2415537537355355515))
# will print:
# 2.555K
# 241.555K
# 241.535555M
# 2.415533347615T
# 2415537.5373553555T
The humanize library does a pretty good job of this.
https://python-humanize.readthedocs.io/en/latest/number/
For example
intword("1000000")
returns
1.0 million
This question already has answers here:
What does the ** maths operator do in Python?
(5 answers)
Closed 1 year ago.
I want to find the 5th power of a number given by the user. Can i do this without typing:
ans = n * n * n * n * n
and can it be used for higher powers?
Use the ** operator for exponents:
print(5**12)
>>> 224832
We can use this more generally using variables.
exponent = 5
n = 12
ans = n ** exponent
print(ans)
>>> 248832
Other Methods
Since the ** operator caps at an exponent of 256, we can use the built-in pow() function for larger exponents. However, these numbers can be very large.
power = pow(base, exponent)
The math and numpy libraries also can do this using the math.pow() and numpy.power() functions.
An alternative to the ** operator is to use the math library functions.
import math
math.pow(x, y)
Note that this returns a floating point value, while the ** operator will return an int or float based on the type of x.
This question already has answers here:
>> operator in Python
(5 answers)
Closed 6 years ago.
Someone sent me this equation but I don't understand what it means.
result = ((~c1) >> 1) & 0x0FFFFFF
It has to do with converting the binary from a wiegand reader.
Reference
The >> operator in Python is a bitwise right-shift. If your number is 5, then its binary representation is 101. When right-shifted by 1, this becomes 10, or 2. It's basically dividing by 2 and rounding down if the result isn't exact.
Your example is bitwise-complementing c1, then right-shifting the result by 1 bit, then masking off all but the 24 low-order bits.
This statement means:
result = # Assign a variable
((~c1) # Invert all the bits of c1
>> 1) # Shift all the bits of ~c1 to the right
& 0x0FFFFFF; # Usually a mask, perform an & operator
The ~ operator does a two's complement.
Example:
m = 0b111
x = 0b11001
~x == 0b11010 # Performs Two's Complement
x >> 1#0b1100
m == 0b00111
x == 0b11001 # Here is the "and" operator. Only 1 and 1 will pass through
x & m #0b 1
This question already has answers here:
Is there a ceiling equivalent of // operator in Python?
(9 answers)
Closed 7 years ago.
It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes.
Is there a way to divide that rounds upwards if there is a non-zero remainder?
For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division:
items = 102
boxsize = 10
num_boxes = (items + boxsize - 1) // boxsize
Alternatively, use negation to convert floor division to ceiling division:
num_boxes = -(items // -boxsize)
Negate before and after?
>>> -(-102 // 10)
11
from math import ceil
print(ceil(10.3))
11
You can try :
import math
math.ceil( x )