bitflags

How to make sense of O_RDONLY = 0?

你离开我真会死。 提交于 2020-05-23 16:25:26
问题 I am dealing with file status flags. Among test I performed, I found #include <stdio.h> #include "fcntl.h" int main() { const int flag = O_RDONLY; printf( "*** Flag O_RDONLY = %5d\n", flag); return 0; } produces this output *** Flag O_RDONLY = 0 which is fully consistent with #define O_RDONLY 00 from fcntl-linux.h . How can the value zero be used as a flag? I expect an "atomic" flag to be 2^n ( n>=1 ), and "composite" flags (like O_ACCMODE ) to be simply the sum of several atomic flags (which

How to clear the state bits in an iostream object in C++?

☆樱花仙子☆ 提交于 2020-04-30 07:49:11
问题 I'm trying to learn C++ from an older edition of the Primer, and tried to execute some of their code relating to iostream objects, which gave me some trouble: #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { int ival; try { while (cin >> ival, !cin.eof()) { if (cin.bad()) throw runtime_error("IO stream corrupted"); if (cin.fail()) { cerr << "Invalid input - try again"; cin.clear(iostream::failbit); continue; } else cout << ival << endl; } return

How to clear the state bits in an iostream object in C++?

早过忘川 提交于 2020-04-30 07:48:16
问题 I'm trying to learn C++ from an older edition of the Primer, and tried to execute some of their code relating to iostream objects, which gave me some trouble: #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { int ival; try { while (cin >> ival, !cin.eof()) { if (cin.bad()) throw runtime_error("IO stream corrupted"); if (cin.fail()) { cerr << "Invalid input - try again"; cin.clear(iostream::failbit); continue; } else cout << ival << endl; } return

#defined bitflags and enums - peaceful coexistence in “c”

╄→гoц情女王★ 提交于 2019-12-22 00:23:37
问题 I have just discovered the joy of bitflags. I have several questions related to "best-practices" regarding the use of bitflags in C. I learned everything from various examples I found on the web but still have questions. In order to save space, I am using a single 32bit integer field in a struct ( A->flag ) to represent several different sets of boolean properties. In all, 20 different bits are #define d. Some of these are truly presence/absence flags (STORAGE-INTERNAL vs. STORAGE-EXTERNAL).

What Does the [Flags] Attribute Really Do?

南笙酒味 提交于 2019-12-17 16:27:03
问题 What does applying [Flags] really do? I know it modifies the behavior of Enum.ToString, but does it do anything else? (e.g. Different compiler or runtime behavior, etc.) Edit : Yeah, I'm aware that it documents the fact that the enum is intended to be used as bitwise flags, and that it's more logical to apply it to bit flags. I was asking more about concrete behavior changes though, not general programming practices. 回答1: From an MSDN article: It is interesting to note that when Flags is

Determine which single bit in the byte is set

▼魔方 西西 提交于 2019-12-17 13:00:47
问题 I have a byte I'm using for bitflags. I know that one and only one bit in the byte is set at any give time. Ex: unsigned char b = 0x20; //(00100000) 6th most bit set I currently use the following loop to determine which bit is set: int getSetBitLocation(unsigned char b) { int i=0; while( !((b >> i++) & 0x01) ) { ; } return i; } How do I most efficiently determine the position of the set bit? Can I do this without iteration? 回答1: Can I do this without iteration? It is indeed possible. How do I

Bit Manipulation and Flags

你离开我真会死。 提交于 2019-12-13 03:53:25
问题 https://i.imgur.com/VU56Rwn.png A: When the man page for open says: The flags specified are formed by or'ing the following values: O_RDONLY open for reading only O_WRONLY open for writing only ... it means we should use a logical or between the flags like this: O_RDONLY || O_WRONLY to specify the combination of permissions we want. B: To indicate different options we use bit flags (rather than characters or integers) in order to save space. C: Performing operations on bit flags is fast. D:

Testing if a bitmask has one and only one flag

与世无争的帅哥 提交于 2019-12-11 11:47:39
问题 I've been scouring google and stack overflow for an answer to this question and I haven't been able to explicitly find it. How would I test a bitmask to see if it has one and ONLY one flag set to it? I.E It would return false if any other flags were set inside the mask? I know that I can check to see if the mask has any flags with this. (currentFlags & state) == state I figure it's a bit more complex to check if a mask only has one flag. Every site that I've visited, that explains bitmasking,

Bitflag enums in C++

送分小仙女□ 提交于 2019-12-10 23:34:54
问题 Using enums for storing bitflags in C++ is a bit troublesome, since once the enum values are ORed they loose their enum-type, which causes errors without explicit casting. The accepted answer for this question suggests overloading the | operator: FlagsSet operator|(FlagsSet a, FlagsSet b) { return FlagsSet(int(a) | int(b)); } I'd like to know if this method has any runtime implications? 回答1: Runtime implications in terms of correctness? No - this should be exactly what you want. Runtime

Extend Enum with flag methods?

允我心安 提交于 2019-12-10 11:11:11
问题 I have found good examples on how to create extension methods to read out single values from bitwise enums. But now that C# 4 has added the HasFlag method they are really not needed. What I think would be really helpful though is an extension to SET a single flag! I have many situations where I need to set the flag values individually. I want an extension method with this signature: enumVariable.SetFlag(EnumType.SingleFlag, true); OR possibly: enumVariable.SetFlag<EnumType>(EnumType