math with bits
Archiver
Posts: 46,084
Hey everybody. I'm trying out an example from the micro-controller
cookbook, using an ad0831 for A/D conversion. It works great, but I
don't understand one part of the code. First, here's the code
low cs 'select the converter
pulsout ck,1 'send the first "setup clock pulse"
x=0 'set x to 0
for y=1 to 8 'loop eight times to get the 8 data bits
pulsout ck,1 'send a clock pulse
x=x*2 'shift bits once to the left
x=x+in15 'add x to the incoming bit
next 'next loop
high cs 'deselect the ad0831
my question is, how does (x=x*2) allow you to add the correct bit
value on? I should admit that I know nothing about doing math with
binary values, so if someone wants to point me to a primer on the
subject that would cool as well.
Thanks!
cookbook, using an ad0831 for A/D conversion. It works great, but I
don't understand one part of the code. First, here's the code
low cs 'select the converter
pulsout ck,1 'send the first "setup clock pulse"
x=0 'set x to 0
for y=1 to 8 'loop eight times to get the 8 data bits
pulsout ck,1 'send a clock pulse
x=x*2 'shift bits once to the left
x=x+in15 'add x to the incoming bit
next 'next loop
high cs 'deselect the ad0831
my question is, how does (x=x*2) allow you to add the correct bit
value on? I should admit that I know nothing about doing math with
binary values, so if someone wants to point me to a primer on the
subject that would cool as well.
Thanks!
Comments
binary system. For example (% indicates binary value):
%0011 x 2 = %0110
See how the two "1" bits moved left?
In the case you stated, the next bit is always added to the resultant value.
This puts the new bit in the LSB (rightmost) position. The next shift moves
it left.
BTW ... it's much easier to use SHIFTIN with the ADC0831. You can get the
code from the StampWorks book (download as PDF) and a few other places on the
Parallax web site.
-- Jon Williams
-- Parallax
In a message dated 4/25/02 10:52:14 AM Central Daylight Time,
mhallen@p... writes:
> my question is, how does (x=x*2) allow you to add the correct bit
> value on? I should admit that I know nothing about doing math with
> binary values, so if someone wants to point me to a primer on the
>
[noparse][[/noparse]Non-text portions of this message have been removed]
since it's a native PIC instruction, but I'm willing to be corrected. [noparse]:)[/noparse]
For a stamp project I did I had a lot of different light patterns running
over a 64-element light array, I ended up using mostly shifts and bit &'s
and |'s.
On Thu, 25 Apr 2002 jonwms@a... wrote:
> When you multiply a value by 2, it's the same as shifting left one bit in the
> binary system. For example (% indicates binary value):
>
> %0011 x 2 = %0110
>
> See how the two "1" bits moved left?
>
> In the case you stated, the next bit is always added to the resultant value.
> This puts the new bit in the LSB (rightmost) position. The next shift moves
> it left.
>
> BTW ... it's much easier to use SHIFTIN with the ADC0831. You can get the
> code from the StampWorks book (download as PDF) and a few other places on the
> Parallax web site.
>
> -- Jon Williams
> -- Parallax
>
>
> In a message dated 4/25/02 10:52:14 AM Central Daylight Time,
> mhallen@p... writes:
>
>
> > my question is, how does (x=x*2) allow you to add the correct bit
> > value on? I should admit that I know nothing about doing math with
> > binary values, so if someone wants to point me to a primer on the
> >
>
>
>
>
> [noparse][[/noparse]Non-text portions of this message have been removed]
>
>
> To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
> from the same email address that you subscribed. Text in the Subject and Body
of the message will be ignored.
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
Sean T. Lamont, CTO / Chief NetNerd, Abstract Software, Inc. (ServNet)
Seattle - Bellingham - Vancouver - Portland - Everett - Tacoma - Bremerton
email: lamont@a... WWW: http://www.serv.net
"Do not fear mistakes, There Are None" - Miles Davis