Add leading zeros in function bin

This commit is contained in:
vdimir 2021-07-06 15:18:47 +03:00
parent dd06866fa8
commit c6e13e6e2e
No known key found for this signature in database
GPG Key ID: F57B3E10A21DBB31
3 changed files with 8 additions and 37 deletions

View File

@ -1,6 +1,5 @@
#pragma once
#include <string>
#include <bit>
/// Maps 0..15 to 0..9A..F or 0..9a..f correspondingly.
@ -47,20 +46,6 @@ inline void writeBinByte(UInt8 byte, void * out)
memcpy(out, &bin_byte_to_char_table[static_cast<size_t>(byte) * 8], 8);
}
inline size_t writeBinByteNoLeadZeros(UInt8 byte, char * out)
{
if (byte == 0)
return 0;
int clz = std::countl_zero(byte);
for (Int8 offset = sizeof(UInt8) * 8 - clz - 1; offset >= 0; --offset)
{
*out = ((byte >> offset) & 1) ? '1' : '0';
++out;
}
return sizeof(UInt8) * 8 - clz;
}
/// Produces hex representation of an unsigned int with leading zeros (for checksums)
template <typename TUInt>
inline void writeHexUIntImpl(TUInt uint_, char * out, const char * const table)

View File

@ -1260,7 +1260,7 @@ struct HexImpl
{
UInt8 byte = x >> offset;
/// Leading zeros.
/// Skip leading zeros
if (byte == 0 && !was_nonzero && offset)
continue;
@ -1349,26 +1349,12 @@ struct BinImpl
UInt8 byte = x >> offset;
/// Skip leading zeros
if (byte == 0 && !was_nonzero)
if (byte == 0 && !was_nonzero && offset)
continue;
/// First non-zero byte without leading zeros
if (was_nonzero)
{
writeBinByte(byte, out);
out += word_size;
}
else
{
size_t written = writeBinByteNoLeadZeros(byte, out);
out += written;
}
was_nonzero = true;
}
if (!was_nonzero)
{
*out = '0';
++out;
writeBinByte(byte, out);
out += word_size;
}
*out = '\0';
++out;

View File

@ -1,8 +1,8 @@
0
1
1010
1111111
00000000
00000001
00001010
01111111
11111111
00110000
0011000100110000