Welcome to MSDN Blogs Sign in | Join | Help

Visual Studio 2008 Enum Bit Flags Visualization

In Visual Studio 2008 we introduced a useful, but not very well known, feature in the Native Debugger for visualizing enum bit flags. Consider the following code:

enum x {

bit_none = 0,

      bit1 = 1,

      bit2 = (1 << 1),

      bit3 = (1 << 2),

      bit4 = (1 << 3),

bit_all = -1

};

Let’s define a variable:

x flags = static_cast<x>( bit1 | bit2 | bit4 );

In Visual Studio 2005, the Debugger shows the following in a Locals Windows:           

clip_image002

But Visual Studio 2008 is better than that.  Instead, you’ll get this:

clip_image004

which is more intuitive and readable as we see field names instead of integer values.

This feature was specifically designed to visualize bit flag names that are a power of two. As soon as an enum contains members whose values don’t adhere to this requirement, the visualization won’t work since arbitrary numeric values (i.e. those not a power of 2) cannot be interpreted by the debugger in a single way. This rule has two exceptions: we support a value of 0, which indicates no bits set, and a value with all bits set for the underlying type.  In the case of int this value is -1 (yes, it is platform specific J).

The Debugger is the single most used feature of Visual Studio. We are well aware of that and are committed to constant improvements in this area. We are always interested in hearing your ideas for what you’d like to see in the next version of the Visual C++ Debugger.  Let us know if you have any interesting requests!

Thanks,

Vytautas Leonavičius/Visual C++

Published Monday, March 10, 2008 12:22 PM by vcblog

Comments

# MSDN Blog Postings &raquo; Visual Studio 2008 Enum Bit Flags Visualization

Monday, March 10, 2008 7:56 PM by Bruno Martínez

# re: Visual Studio 2008 Enum Bit Flags Visualization

x flags = static_cast<x>( bit1 | bit2 | bit4 );

I don't think that's legal C++.

Monday, March 10, 2008 8:20 PM by Norman Diamond

# re: Visual Studio 2008 Enum Bit Flags Visualization

To Bruno Martinez:  Why not?  It looks to me like 5.2.9 (page 75 first paragraph) addresses exactly that.

To Vytautas Leonavičius:

I wish that the default display of values would be hexadecimal for DWORD, WORD, BYTE, pointers, etc., and decimal for UINT, int, long, etc.  With enums the debugger would have to guess the intended usage, but in your example you could guess.

Monday, March 10, 2008 11:34 PM by C0

# re: Visual Studio 2008 Enum Bit Flags Visualization

Nice.

I agree that the debugger is the single most used feature of Visual Studio. I could say VC's debugger is the only thing that keep me using MS Windows...

Speaking of feature request, is it possible to make autoexp using a scripting language or a dll? In other words, make visualization programmable. The autoexp used now is too weak IMO. If the visualization process is programmable, we could add such enum display trick ourself.

Tuesday, March 11, 2008 9:36 AM by Tonci Tomic

# re: Visual Studio 2008 Enum Bit Flags Visualization

If you are using template classes, even standard one from stl (vector, map), it is almost impossible to see function name in stack trace.

Is it possible to collapse template parameters in type of some template ? Thus, instead of:

std::vector< std::list<int, std::standard_allocator>, ...>::foo<... bla bla>

we should see only:

std::vector<>::foo<>

in stack trace with optional '+' sign near angled brackets expandable on click.

If it is to hard, I would be thankful for option "hide template parameters in watch windows", because, most of the times, I don't need that, space consuming, info.

Tuesday, March 11, 2008 11:15 AM by Dave Johansen

# re: Visual Studio 2008 Enum Bit Flags Visualization

I would love it if you could finally call methods that overloaded with both a standard and const version from the watch window.

Wednesday, March 12, 2008 3:33 AM by QbProg

# re: Visual Studio 2008 Enum Bit Flags Visualization

Please support "operator overloading" in watch window, in particular [] and -> , since these are most important when using Smart Pointers and smart arrays!

Wednesday, March 12, 2008 6:42 AM by Paulius Maruška

# re: Visual Studio 2008 Enum Bit Flags Visualization

Yeah, debugger is very nice in 2008.

I'm also very pleased to see a Lithuanian developer in Visual C++ team! :)

Go Vytautas! ;)

Wednesday, March 12, 2008 9:42 AM by jschroedl

# re: Visual Studio 2008 Enum Bit Flags Visualization

Nice tip! Thanks V. And interesting style of enum declaration - in all my years, I haven't seen it done that way.

John

Wednesday, March 12, 2008 11:05 AM by Bruno Martínez

# re: Visual Studio 2008 Enum Bit Flags Visualization

You are right about the legality.

Sunday, March 16, 2008 8:10 AM by Jonathan Wilson

# re: Visual Studio 2008 Enum Bit Flags Visualization

I would love it if I could add an expression like this:

a->b[i]->c[j] to the watch window where b and/or c are of type std::vector

Tuesday, March 18, 2008 3:42 AM by Robert Neumann

# re: Visual Studio 2008 Enum Bit Flags Visualization

Good Job, Vytautas!

That's a really nice feature.

Thanks,

Robert.

Tuesday, March 18, 2008 3:07 PM by rolkA

# re: Visual Studio 2008 Enum Bit Flags Visualization

I agree with Bruno, thats not legal C++ (actually it IS grammatically legal but this shouldn't be done like this). A combination of enum values have to be stored in the underlying type, int for example, because the result is not an "x" anymore...

Tuesday, March 18, 2008 8:42 PM by Norman Diamond

# re: Visual Studio 2008 Enum Bit Flags Visualization

"the result is not an "x" anymore..."

But the result is still within range.  I guess it's necessary to add section 7.2 paragraph number 6 (page 113 third paragraph) to section 5.2.9 paragraph number 7 (page 75 first paragraph).

Thursday, March 20, 2008 3:30 PM by stof

# re: Visual Studio 2008 Enum Bit Flags Visualization

This does not work if (1<<31) is defined in the enum, not even if the underlying type is __int64!? Thats a shame really, because many enums in our code use this value. What gives?

And in principle, who would care if its legal c++ when the debugger allows for the expression "(x)flags" in the watch window?

Tuesday, March 25, 2008 9:29 AM by A

# re: Visual Studio 2008 Enum Bit Flags Visualization

Where are the GDI, ie. bitmap and graphics visualizers?

You would save yourselves a lot of work with those.

Monday, March 31, 2008 11:52 AM by pete

# re: Visual Studio 2008 Enum Bit Flags Visualization

The images don't seem to show up in Firefox (2.0.0.13)

New Comments to this post are disabled
 
Page view tracker