06_bool类型

本文介绍了C++中bool类型的特性和使用方式。C++在C语言的基础上引入了bool类型,其值域仅包括true和false两种状态。文章通过示例展示了如何定义和使用bool变量,并解释了编译器如何处理非0值和0值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
    新增bool类型
    C++在C语言的基本类型系统之上增加了bool
    C++中的bool可取的值只有true和false
    理论上bool只占用一个字节,
    如果多个bool变量定义在一起,可能会各占一个bit,这取决于编译器的实现

    true  代表真值,  编译器内部用1来表示
    false 代表非真值,编译器内部用0来表示

    bool类型只有true(非0)和false(0)两个值
    C++编译器会在赋值时将非0值转换为true,0值转换为false

*/
#if 1
void test()
{
    int a;
    bool b = true;
    bool b1 = true;  //告诉c++编译器给我分配 1个字节的内存
    bool b2, b3, b4, b5;
    printf("b = %d, sizeof(b) = %d\n", b, sizeof(b));//b = 1, sizeof(b) = 1
    b = 4;
    a = b;
    printf("a = %d, b = %d\n", a, b);//a = 1, b = 1
    //bool变量 1 或者 0
    b1 = -10;
    cout << "bl:" << b1 << endl;//bl:1
    //bool变量 1 或者 0
    b1 = 0;
    cout << "bl:" << b1 << endl;//bl:0
}
#endif
详细讲解下面的代码: { // 准备存放设备名称和 UUID(16-bit 和 128-bit)的缓存区。 /* inquiry result */ bt_bdname_t bdname; uint8_t remote_name_len; uint8_t num_uuids = 0, num_uuids128 = 0, max_num_uuid = 32; uint8_t uuid_list[32 * Uuid::kNumBytes16]; uint8_t uuid_list128[32 * Uuid::kNumBytes128]; // 根据 EIR 数据判断是否需要发起 Remote Name Request(即设备是否已广播出名称)。 p_search_data->inq_res.remt_name_not_required = check_eir_remote_name(p_search_data, NULL, NULL); // 获取发现设备的地址。 RawAddress& bdaddr = p_search_data->inq_res.bd_addr; BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __func__, bdaddr.ToString().c_str(), p_search_data->inq_res.device_type); bdname.name[0] = 0; // 尝试从 EIR 中读取名称,失败则从缓存中获取。 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len)) check_cached_remote_name(p_search_data, bdname.name, &remote_name_len); /* Check EIR for services */ // 获取 UUID(服务)信息: if (p_search_data->inq_res.p_eir) { BTM_GetEirUuidList(p_search_data->inq_res.p_eir, p_search_data->inq_res.eir_len, Uuid::kNumBytes16, &num_uuids, uuid_list, max_num_uuid); BTM_GetEirUuidList(p_search_data->inq_res.p_eir, p_search_data->inq_res.eir_len, Uuid::kNumBytes128, &num_uuids128, uuid_list128, max_num_uuid); } { /* 构建属性列表: 将设备属性打包成数组,逐项填充: - 蓝牙地址 - 名称 - 类别(COD) - 类型(BR/EDR、BLE 或 DUAL) - RSSI - 是否支持 CSIP 协调组 - UUID(服务) UUID 部分还会缓存入 eir_uuids_cache,并使用 btif_update_uuid 做去重更新。 */ bt_property_t properties[7]; bt_device_type_t dev_type; uint32_t num_properties = 0; bt_status_t status; tBLE_ADDR_TYPE addr_type = BLE_ADDR_PUBLIC; memset(properties, 0, sizeof(properties)); /* RawAddress */ BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr); num_properties++; /* BD_NAME */ /* Don't send BDNAME if it is empty */ if (bdname.name[0]) { BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_BDNAME, strlen((char*)bdname.name), &bdname); num_properties++; } /* DEV_CLASS */ uint32_t cod = devclass2uint(p_search_data->inq_res.dev_class); BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod); if (cod != 0) { BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); num_properties++; } /* DEV_TYPE */ /* FixMe: Assumption is that bluetooth.h and BTE enums match */ /* Verify if the device is dual mode in NVRAM */ int stored_device_type = 0; if (btif_get_device_type(bdaddr, &stored_device_type) && ((stored_device_type != BT_DEVICE_TYPE_BREDR && p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) || (stored_device_type != BT_DEVICE_TYPE_BLE && p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) { dev_type = (bt_device_type_t)BT_DEVICE_TYPE_DUMO; } else { dev_type = (bt_device_type_t)p_search_data->inq_res.device_type; } if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE) addr_type = p_search_data->inq_res.ble_addr_type; BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type); num_properties++; /* RSSI */ BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t), &(p_search_data->inq_res.rssi)); num_properties++; /* CSIP supported device */ BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER, sizeof(bool), &(p_search_data->inq_res.include_rsi)); num_properties++; /* Cache EIR queried services */ if ((num_uuids + num_uuids128) > 0) { uint16_t* p_uuid16 = (uint16_t*)uuid_list; auto uuid_iter = eir_uuids_cache.find(bdaddr); Uuid new_remote_uuid[BT_MAX_NUM_UUIDS]; size_t dst_max_num = sizeof(new_remote_uuid)/sizeof(Uuid); size_t new_num_uuid = 0; Uuid remote_uuid[BT_MAX_NUM_UUIDS]; if (uuid_iter == eir_uuids_cache.end()) { auto triple = eir_uuids_cache.try_emplace(bdaddr, std::set<Uuid>{}); uuid_iter = std::get<0>(triple); } //LOG_INFO("EIR UUIDs for %s:", bdaddr.ToString().c_str()); for (int i = 0; i < num_uuids; ++i) { Uuid uuid = Uuid::From16Bit(p_uuid16[i]); //LOG_INFO(" %s", uuid.ToString().c_str()); uuid_iter->second.insert(uuid); if (i < BT_MAX_NUM_UUIDS) { remote_uuid[i] = uuid; } else { LOG_INFO("%d >= %d", i, BT_MAX_NUM_UUIDS); } } for (int i = 0; i < num_uuids128; ++i) { Uuid uuid = Uuid::From128BitBE((uint8_t *)&uuid_list128[i * Uuid::kNumBytes128]); //LOG_INFO(" %s", uuid.ToString().c_str()); uuid_iter->second.insert(uuid); if (i < BT_MAX_NUM_UUIDS) { remote_uuid[num_uuids + i] = uuid; } else { LOG_INFO("%d >= %d", i, BT_MAX_NUM_UUIDS); } } //LOG_INFO("%s %d : update EIR UUIDs.", __func__, __LINE__); new_num_uuid = btif_update_uuid(bdaddr, remote_uuid, (num_uuids + num_uuids128), new_remote_uuid, sizeof(new_remote_uuid), dst_max_num); BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_UUIDS, new_num_uuid * Uuid::kNumBytes128, new_remote_uuid); //LOG_INFO("%s %d : fill BT_PROPERTY_UUIDS property.", __func__, __LINE__); num_properties ++; } // 持久化 & 回调: status = btif_storage_add_remote_device(&bdaddr, num_properties, properties); // 添加进本地数据库 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status); status = btif_storage_set_remote_addr_type(&bdaddr, addr_type); // 保存 BLE 地址类型 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status); bool restrict_report = osi_property_get_bool( "bluetooth.restrict_discovered_device.enabled", false); // 限制上报策略, 可选地根据系统属性是否限制上报某些设备。 if (restrict_report && p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE && !(p_search_data->inq_res.ble_evt_type & BTM_BLE_CONNECTABLE_MASK)) { LOG_INFO("%s: Ble device is not connectable", bdaddr.ToString().c_str()); break; } // 上报到上层(Java 层): 触发 BluetoothAdapter.onBluetoothDeviceFound() 等 Java 层通知。 /* Callback to notify upper layer of device */ invoke_device_found_cb(num_properties, properties); } }
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值