Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: I2C help (from python)

by BrowserUk (Patriarch)
on Nov 30, 2015 at 15:59 UTC ( [id://1148909]=note: print w/replies, xml ) Need Help??


in reply to I2C help (from python)

So it's really this line self.DEVICE_REG_DATA &= ~(0x1<<0) that's causing my trouble.

&= bitwise ANDs the value to the left with the value to the right and replaces the value to the left with the result. Ie. It is equivalent to $left = $left  & $right;. And the perl code would be exactly the same.

The left value is initialised: self.DEVICE_REG_DATA = 0xff. The right value: ~(0x1<<0) is a stupidly complicated way of writing ~1:

[0] Perl> printf "%b\n", ~(1<<0);; 1111111111111111111111111111111111111111111111111111111111111110 [0] Perl> printf "%b\n", ~(1);; 1111111111111111111111111111111111111111111111111111111111111110 [0] Perl> printf "%b\n", ~1;; 1111111111111111111111111111111111111111111111111111111111111110

(The number of ones may vary depending upon the native size of integers in the version of Python being used.

When ANDed with the initialisation value you get:

printf "%b\n", 0xff & ~1;; 11111110

In other words, the result of the &= statement is to clear the least significant bit of the byte, leaving all the other bits as they were.

Does that help you solve your (unclear) question?

BTW. Early in the post you mention &|, but that doesn't appear anywhere else in your post or code? And isn't a valid Python operator from my (rather awkward) quick search.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: I2C help (from python)
by packetstormer (Monk) on Nov 30, 2015 at 16:05 UTC

    Thanks for this

    Regarding the &|, you're correct, it should read |=, sorry about the confusion

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1148909]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found