 #include <ctime>
 #include <cstring>
 
-#define SET_BIT_CHAR_ARRAY(arr, i) ( arr[i/8] |= 1 << (i % 8) )
-#define CLEAR_BIT_CHAR_ARRAY(arr, i) ( arr[i/8] &= ~(char)(1 << (i % 8)) )
-#define READ_BIT_CHAR_ARRAY(arr, i) ( arr[i/8] & 1 << (i % 8) )
+#define INT_SIZE ( sizeof(int) * 8)
+
+#define GET_BITMASK_INT(i) ( 1 << (i % (INT_SIZE)) )
+#define GET_ARRAY_ELEMENT(i) ( i/ (INT_SIZE))
+#define SET_BIT_INT_ARRAY(arr, i) ( arr[i/INT_SIZE] |= 1 << (i % INT_SIZE) )
+#define CLEAR_BIT_INT_ARRAY(arr, i) ( arr[i/INT_SIZE] &= ~(char)(1 << (i % INT_SIZE)) )
+#define READ_BIT_INT_ARRAY(arr, i) ( arr[i/INT_SIZE] & 1 << (i % INT_SIZE) )
 
 
 //WARNING, has to be (x + 1) % cMaxThreads == 0
 const int cMaxThreads = 1024;
-const int cMax = 2047;
+const int cMax = 1023;
 const int cNumBlocks = (cMax + 1) / cMaxThreads;
 const int cResults = cMax / 2 + 1;
-const int cResultCharArraySize = cResults / 8;
+const int cResultArraySizeInBytes = cResults / INT_SIZE + 1;
 
 /*
 #if (((cMax + 1) % cMaxThreads ) != 0)



    // Launch a kernel on the GPU with one thread for each element.
	//calculatePrimeSingle <<<cNumBlocks, cMaxThreads >> >(dev_isPrime);

	clearAll << <cNumBlocks, cMaxThreads >> >(dev_isPrime);
	//setAll << <cNumBlocks, cMaxThreads >> >(dev_isPrime);

    // Check for any errors launching the kernel

#endif */

// memory layout: index * 2 + 1
__global__ void calculatePrimeSingle(int *isPrime) 
{
	int x = threadIdx.x;
	int y = blockIdx.x;
	int i = blockDim.x * y + x;
	int numToCheck = i * 2 + 1;
	SET_BIT_INT_ARRAY(isPrime, i);
	for (int j = 3; j < numToCheck / 2; j += 2) {
		if ((numToCheck % j) == 0) {
			CLEAR_BIT_INT_ARRAY(isPrime, i);
			return;
		}
	}
}

__global__ void clearAll(int *isPrime)
{
	int x = threadIdx.x;
	int y = blockIdx.x;
	int i = blockDim.x * y + x;
	CLEAR_BIT_INT_ARRAY(isPrime, i);
}

__global__ void setAll(int *isPrime)
{
	int x = threadIdx.x;
	int y = blockIdx.x;
	int i = blockDim.x * y + x;
	atomicOr(&isPrime[GET_ARRAY_ELEMENT(x)], GET_BITMASK_INT(x));	
}


// Helper function for using CUDA to add vectors in parallel.
cudaError_t calculatePrimeCuda(int *isPrime)
{
    int *dev_isPrime = 0;
    cudaError_t cudaStatus;


