c++ - How to determine whether a 32-bit/64-bit value has a certain 16-bit value? -


questions

consider following 32-bit , 64-bit values:

  uint32_t ival32 = 0x ab cd 12 34;   uint64_t ival64 = 0x ab cd 12 34 56 78 ca be; 
  1. how determine whether ival32 contains 16-bit (word) value? let's 0xcd12.
  2. how determine whether ival64 contains 16-bit (word) value? let's 0x3456.

update 1 (added later)

  • 0xcd12 word value checked might anywhere in ival32 in every word boundary.
  • 0x3456 word value checked might anywhere in ival64 in every word boundary.

update 2 (added later)

i admit ridiculous mistake in question. in previous example, word values checked not in word boundaries in ival32 , ival64. therefore, correction are:

  • for ival32, word value checked can either 0xabcd or 0x1234. thus, example, 0xcd12 shouldn't found in ival32.
  • for ival64, word value checked can 1 of following: 0xabcd or 0x1234 or 0x5678 or 0xcabe. thus, example, 0xcd12 or 0x3456 or 0x78ca shouldn't found in ival64.

remarks

  • solutions intended used in function searches 16-bit character in unicode string. in x86, function reads 2 characters @ time; , in x64, function reads 4 characters @ time.
  • i ask because noticed implementation of glibc strchr() (that works 8-bit character) tries test longword @ time, didn't understand code well.

bool contains (uint32_t haystack, uint16_t needle) {     return ((haystack & 0xffff) == needle) ||            (((haystack >> 16) & 0xffff) == needle); }  bool contains (uint64_t haystack, uint16_t needle) {     return ((haystack & 0xffff) == needle) ||            (((haystack >> 16) & 0xffff) == needle) ||            (((haystack >> 32) & 0xffff) == needle) ||            (((haystack >> 48) & 0xffff) == needle); } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -