简体   繁体   中英

What is the purpose of the Bitwise & operator

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM