Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1234

Python • boolean conversion problem

$
0
0
Hi,
I try to read bit by bit from a binary data type, created from a decimal number.
The idea is to get bit by bit, convert it to boolean and print the corresponding value, true o false.
I try to get this from the code below. But, unfortunately the conversión of "0" value to boolean yields a "true"??
Could you please let me know what is wrong?

Thanks.

My code:
a=5
b=bin(a)
print(b)
c=b[-1:]
d=b[-2:-1]
e=b[-3:-2]
print(c,bool(c))
print(d,bool(d))
print(e,bool(e))

My output:
0b101
1 True
0 True
1 True

Statistics: Posted by amuntaner — Thu Dec 05, 2024 4:16 pm



Viewing all articles
Browse latest Browse all 1234

Trending Articles