see these result:
i don't know what a will get... can anyone explain to me?Code:int c=10, b=20;
int a = c | b;
now imagine that i only have the a result. how can i get the c and b?
(maybe knowing these, can fix 1 problem ;))
Printable View
see these result:
i don't know what a will get... can anyone explain to me?Code:int c=10, b=20;
int a = c | b;
now imagine that i only have the a result. how can i get the c and b?
(maybe knowing these, can fix 1 problem ;))
| 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
Follow the link D_Drmmr provided and look at XOR
(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?
If you want to just display the binary equivalent of a decimal number then have a look atQuote:
i have some problem convert from decimal to binary... can anyone explain, please?
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;
}
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.
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.Quote:
csbi.wAttributes & 0xff0f for calculate the textcolor and (csbi.wAttributes & 0xfff0) >> 4 for calculate the backcolor?
can anyone explain to me, please?
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/