Some techniques in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Some techniques in C++

  1. #1
    Join Date
    Mar 2018
    Posts
    22

    Some techniques in C++

    in this piece of code bit masking technique and XOR operation are used, could you please explain me about these techniques and what's the alternative ways instead of these methods?

    Code:
    for (i = 1; i <= n; i++) {
        if (i == pow(2, p)) { 
            Final[i] = 0;                 
    	p++;
        } else {
            Final[i] = b[j];
            j++;
        }
    }
    if ((i&j) == i)
        Final[i] ^= Final[j];
    and in the following code also bit masking technique is used, and why 2 for loop is used here? whats the meaning of this lines:

    Code:
    for (i = 1; i < pow(2, r); i = pow(2, x)) {                                                                                          
        for (j = 1; j <= n; j++) {
            if ((i&j) == i)
                Final[i] ^= Final[j];                                                         
        }
        x++;
        std::cout << std::endl << i <<" " <<Final[i] ;
    }
    Last edited by 2kaud; March 30th, 2018 at 04:16 AM. Reason: Formatting for readability

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,132

    Re: Some techniques in C++

    but did you ever read something about bitwise operations?
    At least, something from wiki?
    Or from MSDN?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,160

    Re: Some techniques in C++

    whats the meaning of this lines:
    The best way to understand code is to trace through each statement with the debugger and watch the contents of the variables used. That way you can see the effect of each statement.
    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++17 Compiler: Microsoft VS2017 (15.6.6)

  4. #4
    Join Date
    Feb 2017
    Posts
    257

    Re: Some techniques in C++

    Quote Originally Posted by Kmilano View Post
    in this piece of code bit masking technique and XOR operation are used, could you please explain me about these techniques and what's the alternative ways instead of these methods?
    Using bitwise operations are usually the best way to perform operations on bits so alternatives are seldom necessary . But bitwise coding can be quite obscure and clever so it's never wrong to put in a comment or two as a courtesy to the reader.

    The programmer in this case is using the pow function to produce numbers that are powers of two. This is not a bitwise operation though. The bitwise equivalent to pow(2,p) is (1<<p) which means that a rightmost bit is shifted p positions to the left.

    The equality((i&j) == i) is true when all set bits in i are also set in j. If the bits of i and j are used to indicate set membership this would be to say that i is a subset of j (but that mustn't necessarily be how they're used here).

    The assignment Final[i] ^= Final[j] performs an xor operation of course but why is not clear from the code you posted. Very common uses for xor in applications are

    - scrambling (a kind of simple cryptography),
    - checksum calculations (for data integrity purposes),
    - Nim-sum calculations (to find a winning strategy in certain games).

    But ever so often xor is used simply because that's the wanted logical operation.
    Last edited by wolle; March 31st, 2018 at 05:46 AM.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)