This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Python int to binary string?
(36 answers)
Closed 1 year ago.
eight character (zero and one) string -> turn it into 8 bit binary number -> perform binary operation -> turn back into 8 0/1 string
>>> the_entry = "00000000"
>>> the_binary number = 8CharacterStringIntoThe8CharacterBinaryNumber(the_entry)
>>> the_binary_number << 1 # any binary operation
>>> the_result_string = EightCharacterBinaryNumberIntoEightCharacterString(the_binary_number)
this is soo simple, I refuse to write my own method, but obviously, in th 180000 possible results, I cannot find the one related to my question.
Do you hold the magic??
These are simple basic operations:
entry = "00000000"
number = int(entry, 2)
result = f"{number:08b}"
Related
This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 5 months ago.
I have an input a=int(1010). I want to find integer equivalence of binary 1010. If I use bin(a) then output will be 1111110010, but I want to get 10.
You need to tell python, that your integer input is in binary. You can either parse a string with a defined base, or ad a 0b-prefix to your code constants.
a = int("1010", base=2)
a = 0b1010
print(a) # result: 10
print(bin(a)) # result: 1010
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
This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here
This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 8 years ago.
When I write run the following code in Python:
m = 010001110110100101110110011001010010000001101101011001010010000001100001011011100010000001000001
print "m = ", m
I receive the output:
m = 7772840013437408857694157721741838884340237826753178459792706472536593002623651807233
What's happening? Is Python automatically converting from base 2 to base 10?
You're starting m with 0. Python assumes it's an Octal (see this)
What you're seeing is the decimal representation of the octal number.
If you want to work with that as binary numbers, I'd recommend setting it as an str and then parse it to int specifying that you're parsing something in base 2:
>>> m='0100011'
>>> int(m, 2)
35
Kind of, it interprets a number starting with 0 as octal, so it actually isn't binary in the first place.
In any case, you can make it output the integer as binary like so:
print "m = ", "{0:b}".format(m)
This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 9 years ago.
I want to convert a string consists of binary digits and convert that string into a binary number.
suppose, my input is "01001", this is in string format, i want to convert this to binary number format to perform various bit wise operations on that.
You can convert the number to an int and do bitwise operations, like this
my_int = int("01001", 2)
print my_int & 1 # 1
print my_int & 8 # 8
print my_int & 16 # 0