098_bit_manipulation2.zig 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Another useful practice for bit manipulation is setting bits as flags.
  2. // This is especially useful when processing lists of something and storing
  3. // the states of the entries, e.g. a list of numbers and for each prime
  4. // number a flag is set.
  5. //
  6. // As an example, let's take the Pangram exercise from Exercism:
  7. // https://exercism.org/tracks/zig/exercises/pangram
  8. //
  9. // A pangram is a sentence using every letter of the alphabet at least once.
  10. // It is case insensitive, so it doesn't matter if a letter is lower-case
  11. // or upper-case. The best known English pangram is:
  12. //
  13. // "The quick brown fox jumps over the lazy dog."
  14. //
  15. // There are several ways to select the letters that appear in the pangram
  16. // (and it doesn't matter if they appear once or several times).
  17. //
  18. // For example, you could take an array of bool and set the value to 'true'
  19. // for each letter in the order of the alphabet (a=0; b=1; etc.) found in
  20. // the sentence. However, this is neither memory efficient nor particularly
  21. // fast. Instead we take a simpler way, very similar in principle, we define
  22. // a variable with at least 26 bits (e.g. u32) and also set the bit for each
  23. // letter found at the corresponding position.
  24. //
  25. // Zig provides functions for this in the standard library, but we prefer to
  26. // solve it without these extras, after all we want to learn something.
  27. //
  28. const std = @import("std");
  29. const ascii = std.ascii;
  30. const print = std.debug.print;
  31. pub fn main() !void {
  32. // let's check the pangram
  33. print("Is this a pangram? {?}!\n", .{isPangram("The quick brown fox jumps over the lazy dog.")});
  34. }
  35. fn isPangram(str: []const u8) bool {
  36. // first we check if the string has at least 26 characters
  37. if (str.len < 26) return false;
  38. // we uses a 32 bit variable of which we need 26 bit
  39. var bits: u32 = 0;
  40. // loop about all characters in the string
  41. for (str) |c| {
  42. // if the character is an alphabetical character
  43. if (ascii.isASCII(c) and ascii.isAlphabetic(c)) {
  44. // then we set the bit at the position
  45. //
  46. // to do this, we use a little trick:
  47. // since the letters in the ASCI table start at 65
  48. // and are numbered by, we simply subtract the first
  49. // letter (in this case the 'a') from the character
  50. // found, and thus get the position of the desired bit
  51. bits |= @as(u32, 1) << @truncate(u5, ascii.toLower(c) - 'a');
  52. }
  53. }
  54. // last we return the comparison if all 26 bits are set,
  55. // and if so, we know the given string is a pangram
  56. //
  57. // but what do we have to compare?
  58. return bits == 0x..???;
  59. }