             }
 
             if (x + 1 < labels.x) {
-                *reinterpret_cast<long long *>(labels.data + labels_index) = (static_cast<long long>(((foreground << 30) >> 31) & label) << 32) | (((foreground << 31) >> 31) & label);
+                *reinterpret_cast<unsigned long long *>(labels.data + labels_index) =
+                    (static_cast<unsigned long long>(((foreground >> 1) & 1) * label) << 32) | (((foreground >> 0) & 1) * label);
 
                 if (y + 1 < labels.y) {
-                    *reinterpret_cast<long long *>(labels.data + labels_index + labels.stepy / labels.elem_size) = (static_cast<long long>(((foreground << 28) >> 31) & label) << 32) | (((foreground << 29) >> 31) & label);
+                    *reinterpret_cast<unsigned long long *>(labels.data + labels_index + labels.stepy / labels.elem_size) =
+                        (static_cast<unsigned long long>(((foreground >> 3) & 1) * label) << 32) | (((foreground >> 2) & 1) * label);
                 }
 
                 if (z + 1 < labels.z) {
-                    *reinterpret_cast<long long *>(labels.data + labels_index + labels.stepz / labels.elem_size) = (static_cast<long long>(((foreground << 26) >> 31) & label) << 32) | (((foreground << 27) >> 31) & label);
-                    
+                    *reinterpret_cast<unsigned long long *>(labels.data + labels_index + labels.stepz / labels.elem_size) =
+                        (static_cast<unsigned long long>(((foreground >> 5) & 1) * label) << 32) | (((foreground >> 4) & 1) * label);
+
                     if (y + 1 < labels.y) {
-                        *reinterpret_cast<long long *>(labels.data + labels_index + labels.stepz / labels.elem_size + (labels.stepy / labels.elem_size)) = (static_cast<long long>(((foreground << 24) >> 31) & label) << 32) | (((foreground << 25) >> 31) & label);
-                    }
-                    
+                        *reinterpret_cast<unsigned long long *>(labels.data + labels_index + labels.stepz / labels.elem_size + (labels.stepy / labels.elem_size)) =
+                            (static_cast<unsigned long long>(((foreground >> 7) & 1) * label) << 32) | (((foreground >> 6) & 1) * label);
+                    }
+
                 }
             }
             else {
-                labels[labels_index] = ((foreground << 31) >> 31) & label;
+                labels[labels_index] = ((foreground >> 0) & 1) * label;
 
                 if (y + 1 < labels.y) {
-                    labels[labels_index + (labels.stepy / labels.elem_size)] = ((foreground << 29) >> 31) & label;
+                    labels[labels_index + (labels.stepy / labels.elem_size)] = ((foreground >> 2) & 1) * label;
                 }
 
                 if (z + 1 < labels.z) {
 
-                    labels[labels_index + labels.stepz / labels.elem_size] = ((foreground << 27) >> 31) & label;
+                    labels[labels_index + labels.stepz / labels.elem_size] = ((foreground >> 4) & 1) * label;
 
                     if (y + 1 < labels.y) {
-                        labels[labels_index + labels.stepz / labels.elem_size + (labels.stepy / labels.elem_size)] = ((foreground << 25) >> 31) & label;
+                        labels[labels_index + labels.stepz / labels.elem_size + (labels.stepy / labels.elem_size)] = ((foreground >> 6) & 1) * label;
                     }
 
                 }


                if ((HasBit(P, 20) && plane_data[-1]) || (HasBit(P, 24) && plane_data[img.stepy - 1]) || (HasBit(P, 36) && plane_data[img.stepz - 1]) || (HasBit(P, 40) && plane_data[img.stepz + img.stepy - 1])) {
                    Union(labels.data, labels_index, labels_index - 2);
                }                
            }
            
        }
    }


        if (x < labels.x && y < labels.y && z < labels.z) {

            int label;
            unsigned char foreground;
            unsigned long long buffer;

            if (x + 1 < labels.x) {
                buffer = *reinterpret_cast<unsigned long long *>(labels.data + labels_index);
                label = (buffer & (0xFFFFFFFF)) + 1;
                foreground = (buffer >> 32) & 0xFFFFFFFF;
            }
