Operands & Syntax

Bryang

Member
Hi all,
I often finish up using bits of code that's on the forum in some of my programming, but quite often there are bits & pieces in there that I don't understand, or spend a while trying to find out, primarily when it comes to using operands, etc. I have had a few hunts on the Rev-ed site in th epast, etc, but havent't found an easy-to-view list, something similar to the basic commands .pdf's etc. although some are used within these tutorial/reference .pdf's.

What I mean are things like...

'...used for entering remarks to explain coding

;...also used for remarks

%... % means BINARY number, eg:%1101 = 13 in Decimal. You can also use binary numbers to "mask" pins or characters in variables - ie: ignor or utilise certain characters in the variable to (eg:)turn things on & off.

$... $ means HEX number, eg: $2E = 46 in decimal. Often used when communicating in i2c serial links.

#... I don't know where the # comes into play!

/... used as division. eg: b1/b2 is value in variable b1 divided by the value in variable b2

//... I haven't worked out what the two backslash symbols together do, but I know they are for example used in breaking down a number into individual digits, eg: to display on an LCD

|... another one in my 'must track this one down one day list!

&... I'm not sure how the concantenation works in PIC basic. eg: in the tutorial it is used in the LDC example, like 'let pins = b2 & 240' I don't know exactly what the & is doing, but the code works a treat!

so when I come across a few of these together, it really gets me scratching my knoggin!!
eg: pins = byte & %11110000 | rsbit
Right!!! OK, must be time for another strong coffee!


The PIC-Basic commands is fine, and generally quite well dcumented with examples, but can anyone point me to a ready reckoner source for the use of these operands?

Cheers, Bryan
 

hippy

Ex-Staff (retired)
I'm sure most of the information is in the manuals, but that's no reason not to have a 'syntax overview'.

<b># </b>

Used exclusively in SERIN, SEROUT and SERTXD to indicate that a variable should be sent / read as a sequence of characters representing a number rather than a single character / raw byte indicated by the value in the variable.

<b>// </b>

And also % when not immediately followed by a 1 or 0.

This is the 'modulus' operator, which returns the reminder of a value after division ...<code><pre><font size=2 face='Courier'>9 / 4 gives a value of 2
9 // 4 gives a value ( remainder ) of 1 </font></pre></code> <b>&amp; </b>

Not a concatenation as in Visual Basic, but the bit-wise AND operator. For every bit in the result, the bit will be set to a 1 if the equivalent bits in both operands are each 1.

<b>| </b>

The bit-wise OR operator. For every bit in the result, the bit will be set to a 1 if an equivalent bit in either operand is a 1.
 

Bryang

Member
Thanks Hippy,
suddenly all has become clear with // :)

Can you give me an example for &amp; and | ?

Maybe Technical can add a couple of pages to the PICAXE Manual 2 - Basic Commands .pdf one day?

Thanks, Bryan.
 

Bryang

Member
Thanks tarzan.
I don't know how I've missed this, as I'm constantly flicking through the 3 manuals!!! Probably I assumed I knew how to use a LET statement &amp; didn't notice the rest of the info on the page below!

another example explaining the &amp; would still be nice though!:)

Bryan.
 

SD2100

New Member
When ANDing one number with another a 1 is put in the result when two bits match as in the example below. bit2 and bit5 in b0 match with bit2 and bit5 in b2 so a 1 is placed in those bit positions in the result, so 165 &amp; 60 = 36.
<code><pre><font size=2 face='Courier'>

b0 = 0
b1 = %10100101 '165
b2 = %00111100 ' 60
' -------------
' 00100100 ' 36

b0 = b1 &amp; b2
</font></pre></code>


Edited by - Phil75 on 26/12/2006 04:02:07
 

hippy

Ex-Staff (retired)
An example for &amp; and | is one you've already cited ...<code><pre><font size=2 face='Courier'>pins = byte &amp; %11110000 | rsbit </font></pre></code> Here 'byte' is anded with %1111000. Because the result bits can only be 1 whe both bits are 1 the bottom four zeroes force the bottom four bits of the result to be zero, the top four bits being 1 let the top bits in 'byte' through. So if ...<code><pre><font size=2 face='Courier'>%10111011 = byte
%00000100 = rsbit </font></pre></code> <code><pre><font size=2 face='Courier'>%10111011 = byte
%11110000
=========
%10110000 = byte &amp; %11110000
%00000100 = rsbit
=========
%10110100 = byte &amp; %11110000 | rsbit </font></pre></code>
 
Top