the csbi.wAttributes are combinations of '|' operators,...

DWORD textcolor = csbi.wAttributes & 0xff0f;
DWORD backcolor = (csbi.wAttributes & 0xfff0) >> 4;

i understand the 0xff0f is a hexadecimal value(i don't know in decimal), but why these number and not other?
can anyone explain to me these 2 calculations?
(they are changed with SetConsoleTextAttribute() function)

3
Contributors
7
Replies
23
Views
4 Years
Discussion Span
Last Post by cambalinho

but why these number and not other?

Magic numbers for bit twiddling are typically chosen to be unique bit patterns. For example, in the case of flags that are intended to be OR'd together you'd typically want to choose numbers where a single bit is set:

0x01 = 0000 0001
0x02 = 0000 0010
0x04 = 0000 0100
0x08 = 0000 1000
0x10 = 0001 0000
0x20 = 0010 0000

That way when they're OR'd together, you get a bit pattern that can easily be tested with AND.

The problem with your question is that you shouldn't be using magic numbers at all. Rather, you should be OR'ing together the manifest constants for those flags, even if it's more verbose. The constants are there because the magic numbers could change, and then code that uses the magic numbers would break.

i don't know these calculations(someone did them for help me)... i need to understand them because seems to be used in very situations.
thanks for all, but please give more information about these type of calculations.
thanks

i need to understand them because seems to be used in very situations.

In this case you don't. Someone gave you bad advice.

please give more information about these type of calculations.

First you need to figure out what those magic numbers represent. It's harder because they're intended to be opaque and not worked with directly here. Once you find out what each of the constants you want to work with represent, it's simple enough to figure out what the bit operations do for you using the corresponding truth tables.

so the functions have there own way for choose the bits.... understood
can you, please, give me a link for read more about that magic numbers?

You dont want to use magic numbers. You want to use the constants that are supplied.

give me a link for read more about that magic numbers?

Magic numbers are just that, magic. They're selected completely at the discretion of the programmer, so there's really nothing to document in general about magic numbers beyond. A good library should hide any use of magic numbers from you anyway.

What I've been saying is that you need a very specific case to study, but this case is not a good one because merely using the magic numbers is a bad practice.

Instead, I think you'd be better off studying bit twiddling in general, rather than continuing with this specific usage. Try googling for "bit manipulation exercises".

thanks for all

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.