# how calculating values from '|' results?

• November 5th, 2013, 02:34 PM
Cambalinho
how calculating values from '|' results?
see these result:

Code:

```int c=10, b=20; int a = c  | b;```
i don't know what a will get... can anyone explain to me?

now imagine that i only have the a result. how can i get the c and b?
(maybe knowing these, can fix 1 problem ;))
• November 5th, 2013, 02:48 PM
D_Drmmr
Re: how calculating values from '|' results?
• November 5th, 2013, 03:10 PM
Cambalinho
Re: how calculating values from '|' results?
Quote:
i need convert the 10 and 20 to binary and then i do the math

10 = 1010 in binary
20 = 10100 in binary

1010 | 10100 = 10100 = 20
tell me if it's corrected
• November 5th, 2013, 03:29 PM
GCDEF
Re: how calculating values from '|' results?
| is the bitwise or operator. The corresponding bit in the result will be turned on if the bit is turned on in either of the operands.

01010 |
10100 =
11110
• November 5th, 2013, 03:33 PM
Cambalinho
Re: how calculating values from '|' results?
Quote:

Originally Posted by GCDEF
| is the logical or operator. The corresponding bit in the result will be turned on if the bit is turned on in either of the operands.

01010 |
10100 =
11110

and i see my mistake: you put '0' on left. thanks for correct me.
now heres the question that i don't know: now from 30 (=11110), imagine that i need the value 01010(=10). how can i get it?
• November 5th, 2013, 03:39 PM
GCDEF
Re: how calculating values from '|' results?
• November 5th, 2013, 04:08 PM
2kaud
Re: how calculating values from '|' results?
• November 5th, 2013, 04:17 PM
Cambalinho
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?
• November 5th, 2013, 04:31 PM
2kaud
Re: how calculating values from '|' results?
Quote:

i have some problem convert from decimal to binary... can anyone explain, please?
If you want to just display the binary equivalent of a decimal number then have a look at
http://programmingknowledgeblog.blog...cimals-to.html

If you want to convert a decimal number to a character string, this can be done using _itoa (or better using _itoa_s)
http://msdn.microsoft.com/en-us/libr.../yakksftt.aspx
http://msdn.microsoft.com/en-us/libr.../0we9x30h.aspx

Code:

```#include <iostream> #include <cstdlib> using namespace std; int main() { char bin[100]; long dec;     cout << "Enter the decimal to be converted:";     cin >> dec;         _itoa(dec, bin, 2);         cout << bin;       return 0; }```
• November 5th, 2013, 04:40 PM
Cambalinho
Re: how calculating values from '|' results?
Quote:

Originally Posted by 2kaud
If you want to just display the binary equivalent of a decimal number then have a look at
http://programmingknowledgeblog.blog...cimals-to.html

If you want to convert a decimal number to a character string, this can be done using _itoa (or better using _itoa_s)
http://msdn.microsoft.com/en-us/libr.../yakksftt.aspx
http://msdn.microsoft.com/en-us/libr.../0we9x30h.aspx

Code:

```#include <iostream> #include <cstdlib> using namespace std; int main() { char bin[100]; long dec;     cout << "Enter the decimal to be converted:";     cin >> dec;         _itoa(dec, bin, 2);         cout << bin;       return 0; }```

thanks for all... thanks
(see if you can explain to me that calculation, please)
• November 5th, 2013, 04:40 PM
razzle
Re: how calculating values from '|' results?
Quote:

Originally Posted by Cambalinho
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.
• November 5th, 2013, 04:44 PM
2kaud
Re: how calculating values from '|' results?
Quote:

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.
• November 5th, 2013, 04:47 PM
Cambalinho
Re: how calculating values from '|' results?
Quote:

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.

sorry, what means '0xfff0'?
• November 5th, 2013, 04:56 PM
2kaud
Re: how calculating values from '|' results?
A number starting 0x means a hexadecimal number (a number system using base 16). The digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f where a b c d e f mean 10, 11, 12, 13, 14, 15 respectfully.

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.

See
http://www.cplusplus.com/doc/hex/