CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    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 )

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: how calculating values from '|' results?

    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how calculating values from '|' results?

    like i read before....
    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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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
    Last edited by GCDEF; November 5th, 2013 at 04:33 PM.

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how calculating values from '|' results?

    Quote Originally Posted by GCDEF View Post
    | 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?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: how calculating values from '|' results?

    Follow the link D_Drmmr provided and look at XOR

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how calculating values from '|' results?

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Apr 2009
    Posts
    1,355

    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?
    Last edited by Cambalinho; November 5th, 2013 at 05:29 PM.

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how calculating values from '|' results?

    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;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how calculating values from '|' results?

    Quote Originally Posted by 2kaud View Post
    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)

  11. #11
    Join Date
    Jul 2013
    Posts
    576

    Re: how calculating values from '|' results?

    Quote Originally Posted by Cambalinho View Post
    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.

  12. #12
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how calculating values from '|' results?

    Quote Originally Posted by 2kaud View Post
    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'?

  14. #14
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured