小编riv*_*run的帖子

C 宏设置多个位

我正在使用 C 语言开发微控制器。其中一部分涉及更改寄存器中的位值。我想出了一些宏来使事情变得更容易:

#define BIT_SET(addr, shift) (addr | (1 << shift))
#define BIT_RESET(addr, shift) (addr & ~(1 << shift))
#define BIT_GET(addr, shift) (addr & (1 << shift))
...
reg1 = BIT_SET(reg1, 3); //set bit 3
...
Run Code Online (Sandbox Code Playgroud)

但是,现在我想制作一个可以一次更改几位的宏。例如,可能想要将 的前 3 位更改10101010110,从而得到数字11001010

我可以在 C 中做到这一点吗?还是我以错误的方式接近这个?

c macros c-preprocessor

5
推荐指数
1
解决办法
3055
查看次数

为STL映射重载<<和>>:"无法绑定左值"

我正在尝试使用fstream将包含自定义对象的STL地图保存到文件中.我使用<<和>>运算符重载这样做.

#include <iostream>
#include <string>
#include <map>
#include <fstream>

#define DELIM '/'

struct Point{
public:
    int x, y;
};
//operator overloads for point
std::istream& operator>>(std::istream& is, Point& p){
    std::string input;
    std::getline(is, input, DELIM);
    p.x = std::stoi(input);
    std::getline(is, input, DELIM);
    p.y = std::stoi(input);
    return is;
}
std::ostream& operator<<(std::ostream& os, Point& p){
    os << p.x << DELIM << p.y << DELIM;
    return os;
}

//operator overloads for map<string, point>
std::istream& operator>>(std::istream& is, std::map<std::string, Point>& m){
    std::string input;
    std::getline(is, input, DELIM);
    int map_size …
Run Code Online (Sandbox Code Playgroud)

c++ gcc stl c++11

3
推荐指数
1
解决办法
421
查看次数

标签 统计

c ×1

c++ ×1

c++11 ×1

c-preprocessor ×1

gcc ×1

macros ×1

stl ×1