         }
 
         #if USE_UTF16
-            yaml_emitter_set_encoding(&emitter, YAML_UTF16BE_ENCODING)
+            yaml_emitter_set_encoding(&emitter, isLittleEndian ? YAML_UTF16LE_ENCODING : YAML_UTF16BE_ENCODING)
         #else
             yaml_emitter_set_encoding(&emitter, YAML_UTF8_ENCODING)
         #endif

        case .initialized:
            var event = yaml_event_t()
            #if USE_UTF16
                yaml_stream_start_event_initialize(&event, YAML_UTF16BE_ENCODING)
            #else
                yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING)
            #endif

        try emit(&event)
    }
}

private let isLittleEndian = 1 == 1.littleEndian

