If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: how calculating values from '|' results?

(someone forget telling us, in tutorials, that we must convert the values to binary for use the bitwise operators... was my 1st confusion... you can see from the thread begining)
or (|) - if 1 or both operators are '1', the result is '1'... else is '0';
xor (^) - if 1 of operators are '1' then the result is '1'. if both operators are the same, the result is '0'.

11110
10100 ^
_______
01010 = 10

11110
01010 ^
________
10100 = 20

ok... now i understand.... i have some problem convert from decimal to binary... can anyone explain, please?

i continue with 1 question:

the csbi.wAttributes is a combination of several '|': some for backcolor and others for textcolor. but why csbi.wAttributes & 0xff0f for calculate the textcolor and (csbi.wAttributes & 0xfff0) >> 4 for calculate the backcolor?
can anyone explain to me, please?

Re: how calculating values from '|' results?

now from 30 (=11110), imagine that i need the value 01010(=10). how can i get it?

I don't think that's possible since OR doesn't have an inverse. Say you do x OR y and get z. The truth table looks like this,

x OR y = z
------------
0 0 0
0 1 1
1 0 1
1 1 1

These are all possibilities of x OR'ed with y giving z.

What you're asking for is some operation between y and z that would give x back. Basically you're asking for this truth table,

x = y ? z
------------
0 0 0
0 1 1 (*)
1 0 1
1 1 1 (*)

But that's not possible due to the two lines marked with (*). When y and z are both 1 you don't know whether x is supposed to be 0 or 1. So the hypothetical ? operation that would be an inverse to OR doesn't exist.

Re: how calculating values from '|' results?

csbi.wAttributes & 0xff0f for calculate the textcolor and (csbi.wAttributes & 0xfff0) >> 4 for calculate the backcolor?
can anyone explain to me, please?

You haven't posted the code containing these statements, but the foreground colors are defined by the first 4 bits (0- 3) and the background colour by the next 4 bits (4 - 7). Considering this as binary (most signifiant on left) we have bbbbffff and as hex 0xbf where b is background and f is foreground. So to just get the foreground colour use & 0x0f which gives the least significant 4 bits (ie the foregound). To get the background use 0xf0 >> 4 which shifts the 4 bit background colour in bits 4 - 7 right 4 bits which puts them in bits 0 - 3 and so can be treated as a colour in the range 0 - 15.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

Re: how calculating values from '|' results?

Originally Posted by 2kaud

You haven't posted the code containing these statements, but the foreground colors are defined by the first 4 bits (0- 3) and the background colour by the next 4 bits (4 - 7). Considering this as binary (most signifiant on left) we have bbbbffff and as hex 0xbf where b is background and f is foreground. So to just get the foreground colour use & 0x0f which gives the least significant 4 bits (ie the foregound). To get the background use 0xf0 >> 4 which shifts the 4 bit background colour in bits 4 - 7 right 4 bits which puts them in bits 0 - 3 and so can be treated as a colour in the range 0 - 15.

In the program posted in post #9, change the 2 in the _itoa line to 16 and it will convert decinal numbers to base 16 (hex). When working with some types of numbers where different bits have special meanings, hexadecimal notation is often used rather than decimal as the bit patterns can be more clearly identified.