Home > Software design >  What is the purpose of the Bitwise & operator
What is the purpose of the Bitwise & operator

Time:05-31

I am working on an application that communicates via ble. It takes the users inputted password and converts it to a byte list before sending it over to the ble device.

List<int> _passWordToByteList(int password) {
  List<int> ret = [];
  ret.add(password & 0xff);
  ret.add((password >> 8) & 0xff);
  ret.add((password >> 16) & 0xff);
  ret.add((password >> 24) & 0xff);

  return ret;
}

I've read the dart documentation multiple times but I can't seem to understand what the purpose of the operand is for and why we would need it to convert the password to a byte list.

CodePudding user response:

BLE sends data over the air in bytes. This means that values larger than 255 have to be sent as series of bytes. BLE sends those bytes in little endian format. The code in your question is taking an int32 and converting it into a list of 4 bytes in little endian format.

The conversion can be done as in your example although the Dart language does offer libraries to help with this:

https://api.dart.dev/stable/2.16.1/dart-typed_data/dart-typed_data-library.html

Example of how to use this is at:

Convert int32 to bytes List in dart

  • Related