Nodejs buffer bitwise slicing

I'm transmitting data through bluetooth LE from a chip to node.js server.

Firmware code:

uint16_t txBuf[5] top &= 0x3FF; bottom &= 0x3FF; txBuf[0] = top + (bottom << 10); txBuf[1] = bottom >> 2;

Basically, the first 10 bit is top, and the next 10 bit is bottom. I could print the buffer in node.js:

console.log(buffer) console.log(buffer.data) <Buffer cd d3 8d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00> 20

How can I parse this in javascript and node.js without doing bit manipulation?


Not sure why you don't want to do bit manipulation. JavaScript can do bit manipulation fine. The C bit manipulation stuff might not even need to be changed, or only a little.

JavaScript typed arrays may speed things up a bit. Double check your Node version and see the Buffer docs. They have some methods like readUInt8 etc. that might help.

Also you can manipulate bits as string if its easier (and if its not too slow), then use parseInt('01010101',2) to convert to a number. Also .toString(2) to convert to binary.


Read your unsigned int from the buffer using included operations (https://nodejs.org/api/buffer.html#buffer_buf_readuintle_offset_bytelength_noassert)

Use essentially the same bitwise operators that you did in C (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators)

Note that >>> is the non-sign extending shift, which will be important in this case if you don't to randomly get a bunch of 1s instead of 0s!


Well you could use the new Uint1Array "JavaScript's missing TypedArray" which is basically a bit field, with the same API as every other Typed Array.

So in your case:

const Uint1Array = require('uint1array'); const bits = new Uint1Array( new Uint8Array(txBuf).buffer ); const top = bits.slice(0,10); const bottom = bits.slice(10,20);


  • How to get minimal absolute path from relative path without any permission checks?
  • Channel mix with Pillow
  • Get attributes of existing SVG elements and bind as data with d3.js
  • Google Cloud Platform - Vanity Nameservers
  • date.js Parse method overrides Javascript Parse method
  • ViewData, ViewBag and TempData violates MVC? [closed]
  • How do I pass worksheet and ranges as variables?
  • Modifying files nested in tar archive
  • Creating interactive SVG maps for a web portal
  • C++ String tokenisation from 3D .obj files
  • Determining the length of a read stream in node js
  • How to open multiple instances of a program in Linux
  • incomplete type 'struct' error in C
  • Efficient algorithm to find additions and removals from 2 collections
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • what does prefix @- mean in makefile?
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • What Makes These Two Array Adds Different?
  • How can Delete be both a DDL and a DML statement
  • KnockoutObservableArray with typed elements in TypeScript
  • Possible to “watch” both HAML and SASS at the same time?
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • npm 5.4.1 install/uninstall all failing
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Using a canvas object in a thread to do simple animations - Java
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Yii2: Config params vs. const/define
  • Spring Data JPA custom method causing PropertyReferenceException
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • HTML download movie download link
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Excel - Autoshape get it's name from cell (value)
  • Updating server-side rendering client-side
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How can I estimate amount of memory left with calling System.gc()?
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • Setting background image for body element in xhtml (for different monitors and resolutions)