# B4R QuestionTranslate Code (Logical AND......)

#### mw71

##### Active Member
hi,

i would like "Translate" this Code to B4R:
https://www.qrp-labs.com/images/synth/demo4/oscillator.ino

i have problem with some Lines:
B4X:
``````Si5351a_Write_Reg (26, (MSNA_P3 & 65280) >> 8);
'-> Logical AND and SHIFT (?)``````
and with this:
B4X:
``````while (outdivider > 900){            // If output divider out of range (>900) use additional Output divider
R = R * 2;
outdivider = outdivider / 2;
}
if (outdivider % 2) outdivider--;    // finds the even divider which delivers the intended Frequency``````

How can i write this in B4R (or it is better to use the C Code?)

#### sorex

##### Expert
the first one would be

B4X:
``````Bit.ShiftRight(Bit.And(MSNA_P3,65280),8)

or

Bit.And(MSNA_P3,65280) / 256  'destination must be INT``````

second one would be

B4X:
``if outdivider mod 2=0 then outdivider=outdivider-1``

Last edited:

#### mw71

##### Active Member
Thank You!!

i have see a other "Problem":
B4X:
``````Si5351a_Write_Reg (31, ((MSNA_P3 & 983040) >> 12) | ((MSNA_P2 & 983040) >> 16));

'-> Solution (hope it's correct :)

Si5351a_Write_Reg (31, Bit.Or(Bit.ShiftRight(Bit.And(MSNA_P3,983040),12),Bit.ShiftRight(Bit.And(MSNA_P2,983040),16))) ' Parts of MSNA_P3 und MSNA_P1``````
@sorex, you write: destination must be INT
the Sub Si5351a_Write_Reg:
B4X:
``````Sub Si5351a_Write_Reg (regist As Byte, value As ULong)                'Writes "byte" into "regist" of Si5351a via I2C
i2C.WriteTo(SI5351_Adress, Array As Byte(regist))                      ' Writes a byte containing the number of the register
i2C.WriteTo(SI5351_Adress, Array As Byte(value))                        ' Writes a byte containing the value To be written in the register
' Sends the data And ends the transmission
End Sub``````
is this correct? i am not sure with the translate/split from ULong/UInt to Byte

#### sorex

##### Expert
1 seems right.

2 I don't know about the function. is there uLong in B4X?

I guess you'll need to split it up like

B4X:
``````i2C.WriteTo(SI5351_Adress, bit.and(value,255))  'low byte
if the register only supports 0-255 then you only need the first line.

#### emexes

##### Well-Known Member
I agree that your translation of the first case looks correct, assuming that the MSNA values are 32-bit. The mask value 983040 would be much clearer as 0xF0000, so that you can see it gives you the first four bits of the high 16-bit word.

The second case looks plausibly correct too, although:
2a/ I would change the datatype of the value parameter to match the register (seems to be a Byte, not a Ulong) and,
2b/ given the .WriteTo routine looks capable of handling multiple bytes, I would expect to write both regist and value in the same operation, eg:
B4X:
``````Sub Si5351a_Write_Reg (regist As Byte, value As Byte)           'writes "value" into "regist" of Si5351a via I2C
i2C.WriteTo(SI5351_Adress, Array As Byte(regist, value))    'first the register address, then the value
End Sub``````
but if your code works, then obviously my expectation is wrong ;-)

What does the already-working C code look like?

Last edited: