language agnostic - Is there a way to check if an integer's string representation contains a zero by using bitwise operations? -
in c# i've been trying come interesting way accomplish following, without using string representation.
private static bool haszerodigit(int value) { string text = value.tostring(); if (text.contains("0")) { homecoming true; } homecoming false; } my first effort trickery along lines of checking if byte in binary representation of absolute value contains 0 byte, doesn't map correctly. though got me thinking if on right track adjustment how integers map individual characters.
my wrong first attempt:
private static bool haszerodigit(int value) { uint temp = value >= 0 ? (uint)value : (uint)-value; if (temp <= uint16.maxvalue) { ushort temp16 = (ushort)temp; homecoming ~((((temp16 & 0x7f7f) + 0x7f7f) | temp16) | 0x7f7f) != 0; } bool haszero = ~((((temp & 0x7f7f7f7f) + 0x7f7f7f7f) | temp) | 0x7f7f7f7f) != 0; homecoming haszero; } so, there way check if integer contains 0 digit without turning string? goal here computational efficiency.
suggestions , solutions in language welcome.
language-agnostic bit-manipulation data-processing
No comments:
Post a Comment