放入'|'是什么关于功能参数呢? [英] What does putting a '|' on function parameter do?

查看:56
本文介绍了放入'|'是什么关于功能参数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何用C ++编写SDL程序,并且遇到了以下代码:

I am learning how to write SDL program in C++, and I came across this code:

SDL_Renderer *ren = 
    SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//                                                       ^ I have no idea what this means?

我不知道这是否是特定的C ++功能吗?

I don't know if this is a specific C++ feature?

我的猜测来自shell脚本编写背景,暗示它可能是管道(我知道显然不是这样),或者只是按位逻辑OR?

My guess coming from shell scripting background suggests it could be a pipe (I know it's obviously not that), or it's just a bitwise OR?

在与上述代码类似的函数参数中使用 | 是什么意思?

What does a | mean when using it in a function parameter like the above code?

推荐答案

这些是可以设置的标志.在这种情况下, | 指的是按位运算符.

These are flags which you can set. In this instance | refers to the bitwise operator.

在您的示例中,这很方便地使您可以通过单个参数组合多个标志.

In your example, this conveniently allows you to combine multiple flags through a single parameter.

假设两个标志具有以下值:

Suppose the two flags have the following values:

SDL_RENDERER_SOFTWARE = 1 // Binary 0001
SDL_RENDERER_ACCELERATED = 2 // Binary 0010
SDL_RENDERER_PRESENTVSYNC = 4 // Binary 0100

两个逻辑按位或将使 flag 的值保持为6.我们可以轻松地从该值确定使用按位AND设置了哪些标志:

A logic bitwise OR of the two, would leave you with the value 6 for the flag. We can easily determine from this value which flags have been set using bitwise AND.:

flag = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC // flag = 6 (0110)
flag & SDL_RENDERER_SOFTWARE == 0 // Not set
flag & SDL_RENDERER_ACCELERATED == 2 // Set
flag & SDL_RENDERER_PRESENTVSYNC == 4 // Set

请注意,在这里重要的是标志必须是2的幂,以确保所有标志组合都产生唯一的值.

Note that it's important here for the flags to be powers of two, to ensure all flag combinations result in a unique value.

这篇关于放入'|'是什么关于功能参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆