Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

bitwise arithmetic


Eli Sep 16, 2014 07:31 PM

Hi,

I have some C code that converts a string of hex numbers to a floating point number. The string is how our device encodes temperatures from ds18b20 sensors. I don't understand how to convert this C code into CRBASIC.

One of the problems is that I don't see an unsigned int datatype that I can use for it.

The other problem is that I don't see a bitwise OR (|) in CRBASIC, just a bitwise AND (&). Is there anyone who can give me a hand on this?

Thanks,

Eli

Here is the program part in C

if ( (character >=48) && (character <= 90) ) {

Serial.write(character);

byte val = character;

// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
}
else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}

tempBytes[bytesRead] = val;

if (bytesRead > 17) {
break;
}
bytesRead++;
}
}

uint8_t scratchPad[8];

scratchPad[0] = (((int16_t)tempBytes[2]) << 4) | tempBytes[3];
scratchPad[1] = (((int16_t)tempBytes[0]) << 4) | tempBytes[1];

int16_t rawTemperature = (((int16_t)scratchPad[0]) << 8) | scratchPad[1];

float celsius = (float) rawTemperature / 16.0;


thinkitcodeit Sep 17, 2014 08:46 AM

AND OR operate as bitwise in CRBasic (& is for string concatenation). You can use the Long type and create expressions such as these shown in the CRBasic help...

Public SourceInteger As Long, MaskedInteger As Long
Public Bit0 As Boolean, Bit1 As Boolean

SourceInteger = &b1100010

'Checking individual bits with AND
Bit0 = SourceInteger AND 1 ' 2^0 &b1
Bit1 = SourceInteger AND 2 ' 2^1 &b10

'Using a bit mask with AND to return a portion of the source

MaskedInteger = SourceInteger AND &b0111111

...Note that &B can be used to specify a binary number when assigning to a Long.

Log in or register to post/reply in the forum.