Not if you want it to be universal & to report if the LSB of a Number, String, Array or Buffer - that makes it somewhat more complicated and is definitely open to interpretation.
Regarding string - I assume you mean the number represented by the string (and not the LSB of the ascii char in position[0])
Regarding Buffer - I assume you mean the LSB of byte in position [0] and not byte reversed and not read as Int16 or float etc?
On such solution (function code part)...
var input = msg.payload;
var t = (typeof input) + "";
if(t == "object") {
if (Buffer.isBuffer(input)) {
if (!input.length) {
node.warn("Buffer is empty");
return null;
}
input = input[0];
t = (typeof input) + ""; //update type
} else if(Array.isArray(input)) {
if (!input.length) {
node.warn("Array is empty");
return null;
}
input = input[0];
t = (typeof input) + ""; //update type
}
}
switch (t) {
case "number":
msg.payload = getBit(input, 0);
break;
case "string":
let n = parseInt(input);
if (isNaN(n)) {
node.warn("payload is not a number");
return null;
}
msg.payload = getBit(n, 0);
break;
default:
node.warn(`payload type ${t} is not supported`);
return null;
}
return msg;
function getBit(number, bitPosition) {
return (number & (1 << bitPosition)) === 0 ? 0 : 1;
}
Awesome, I think this was a good topic so folks can see the value of bit-wise operators. These are in general very fast opcodes. When I was in Ron Klienmans C class at De Anza. We were asked to determine if a number was odd or even. I knew to use a bit-wise operator to do this. But most folks didn't know about them and made very complex attempts.I am writing a new kind of sort. It is a zipper sort. It pushes the odds to one side and the evens another. There are 2 arrays and the sizeof array is important. So when a value comes in knowing the sizeof the array it is pushed to the left or right of the array a value to examine. If a value doesn't exist its inserted one way, if it does exist it is compares the left and right and ether moves or inserts. It is looking for a optimum value delta of 2. I am just writing it here for fun, but will reduce it to assembler at the end and see what I can do to reduce it to the fewest steps.
At the end you have a left and right side to the pyramid. The you just zipper them both up.