# Test bitmap calculation import Bitwise bitmap_hex = "0203040B25292A16" bitmap_bytes = Base.decode16!(bitmap_hex) IO.puts("Bitmap bytes: #{inspect(bitmap_bytes)}") # Convert to list of bytes byte_list = :binary.bin_to_list(bitmap_bytes) IO.puts("Byte list: #{inspect(byte_list)}") # Calculate which fields should be set using current logic field_numbers = byte_list |> Enum.with_index() |> Enum.flat_map(fn {byte, byte_index} -> for bit_index <- 0..7, (byte &&& (1 <<< (7 - bit_index))) != 0 do 1 + byte_index * 8 + bit_index end end) |> Enum.filter(&(&1 <= 64)) IO.puts("Calculated field numbers: #{inspect(field_numbers)}") # Now check what fields [2, 3, 4, 11, 37, 41, 42] should produce target_fields = [2, 3, 4, 11, 37, 41, 42] IO.puts("\nTarget fields: #{inspect(target_fields)}") # Create bitmap for target fields bitmap_bytes_expected = for byte_idx <- 0..7 do byte_value = for bit_idx <- 0..7, reduce: 0 do acc -> field_number = 1 + byte_idx * 8 + bit_idx if field_number in target_fields do acc ||| (1 <<< (7 - bit_idx)) else acc end end byte_value end |> :binary.list_to_bin() IO.puts("Expected bitmap hex: #{Base.encode16(bitmap_bytes_expected)}") # Check each byte individually IO.puts("\nByte-by-byte analysis:") for {byte, idx} <- Enum.with_index(byte_list) do IO.puts("Byte #{idx}: #{byte} (0x#{Integer.to_string(byte, 16)})") for bit_idx <- 0..7 do bit_set = (byte &&& (1 <<< (7 - bit_idx))) != 0 field_num = 1 + idx * 8 + bit_idx if bit_set do IO.puts(" Bit #{bit_idx}: SET -> Field #{field_num}") end end end