     
     // each thread saves its values into temporary shared buffer
     if(even_code_size) {
-        s_out[remaining_codewords + codeword_offset] = even_code_size + 32 * even_code_value;
+        s_out[remaining_codewords + codeword_offset] = even_code_size + (even_code_value << (32 - even_code_size));
         if(odd_code_size) {
-            s_out[remaining_codewords + codeword_offset + 1] = odd_code_size + 32 * odd_code_value;
+            s_out[remaining_codewords + codeword_offset + 1] = odd_code_size + (odd_code_value << (32 - odd_code_size));
         }
     }
     



__device__ static void
gpujpeg_huffman_gpu_encode_value(int & out_nbits, int & out_cword, const int preceding_zero_count, const int value,
                                 const struct gpujpeg_table_huffman_encoder * const d_table) {
    out_cword = value;
    int absolute = value;


/**
 * Adds up to 32 bits at once.
 * Codeword value must be aligned to left (most significant bits).
 */
__device__ inline void 
gpujpeg_huffman_gpu_encoder_emit_bits(unsigned int & remaining_bits, int & byte_count, int & bit_count, uint8_t * const out_ptr, const unsigned int packed_code_word) {
    // decompose packed codeword into the msb-aligned value and bit-length of the value
    const unsigned int code_word = packed_code_word & ~31;
    const unsigned int code_bit_size = packed_code_word & 31;
    
    // concatenate with remaining bits
    remaining_bits |= code_word >> bit_count;
