简体   繁体   English

按位 & 运算符的目的是什么

[英]What is the purpose of the Bitwise & operator

I am working on an application that communicates via ble.我正在开发一个通过 ble 进行通信的应用程序。 It takes the users inputted password and converts it to a byte list before sending it over to the ble device.它获取用户输入的密码并将其转换为字节列表,然后将其发送到 ble 设备。

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.我已经多次阅读 dart 文档,但我似乎无法理解操作数的用途以及为什么我们需要它将密码转换为字节列表。

BLE sends data over the air in bytes. BLE 以字节为单位在空中发送数据。 This means that values larger than 255 have to be sent as series of bytes.这意味着大于 255 的值必须作为字节序列发送。 BLE sends those bytes in little endian format. BLE 以小端格式发送这些字节。 The code in your question is taking an int32 and converting it into a list of 4 bytes in little endian format.您问题中的代码采用 int32 并将其转换为小端格式的 4 字节列表。

The conversion can be done as in your example although the Dart language does offer libraries to help with this:尽管 Dart 语言确实提供了库来帮助完成转换,但可以像您的示例中那样完成转换:

https://api.dart.dev/stable/2.16.1/dart-typed_data/dart-typed_data-library.html 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 将 int32 转换为 dart 中的字节列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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