Challenge Solution


#1

My challenge solution. It was basically the same idea as others that have posted, but with a slight change.

[code]//
// main.c
// ChaptChallenge38
//
// Created by Matthew Raby on 9/2/14.
// Copyright © 2014 JerryRay Matthew Raby. All rights reserved.
//

#include <stdio.h>

int main(int argc, const char * argv[])
{
// create variable a and set to the max 64bit value
// create variable b and set first bit to 1
// Create variable c set it to the target value to receive
uint64_t a = UINT64_MAX;
uint64_t b = 1;
uint64_t c = 6148914691236517205;

// create for loop initialized to 0 to increment by 2 and be less than 64
// use int i to shift the value of b right 2 every loop.
for (int i = 0; i<64; i+=2) {
    a = a ^ (b << i);
    // On the final shift, check if
    if (i==62 & a == c) {
        printf("Success!!!\n");
    }else if(i==62 & ~a == c){
        printf("Success!!!\n");
    }
}

return 0;

}

[/code]


#2

After I posted I realized the reason I had to get the compliment of a was because I used XOR instead of OR.