lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Try Understand following Python Codes.

Sat Jan 04, 2014 9:00 pm

1) def __command__(self, channel, diff):
d = [0x00, 0x00, 0x00]
d[0] |= 1 << 2
d[0] |= (not diff) << 1
d[0] |= (channel >> 2) & 0x01
d[1] |= ((channel >> 1) & 0x01) << 7
d[1] |= ((channel >> 0) & 0x01) << 6
return d

2) def __analogWrite__(self, channel, value):
d = bytearray(2)
d[0] = 0
d[0] |= (channel & 0x01) << 7
d[0] |= (self.buffered & 0x01) << 6
d[0] |= (not self.gain & 0x01) << 5
d[0] |= (not self.shutdown & 0x01) << 4
d[0] |= (value >> 8) & 0x0F
d[1] = value & 0xFF
self.writeBytes(d)
self.values[channel] = value

3)def __analogRead__(self, channel, diff):
data = self.__command__(channel, diff)
r = self.xfer(data)
return ((r[1] & self.MSB_MASK) << 8) | r[2]


I am not clear on how the above manipulate bytes0, bytes1, bytes2 in terms bit manipulation?

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Try Understand following Python Codes.

Sat Jan 04, 2014 9:53 pm

The snippet is incomplete.

Bit manipulation in Python is pretty much identical to C.

Return to “General programming discussion”