Re: bit manipulation
- Posted by "Unkmar" <L3Euphoria at bellsouth.net> May 25, 2004
- 401 views
Um, this will help, Maybe you can figure out how it works and what it means, Or someone else might explain it. It is set on a 8 bit pattern. I can be adjusted to most any size pattern you want. Just a matter of a little imagination. Because integer limitations and multiplier results certain pattern sizes can't be obtained. Max 31-bit integer. so, the first bit pattern size that can't be reached is 37 bits. Because 37 is the first prime after 31. All primes starting from 37 and multiples of those primes can not be reached. It is just a matter of mathematics... you would have to use other methods to match those oddballed pattern sizes. Lucius L. Hilley III- unkmar
function shift_stream(sequence stream) sequence u_bits sequence result -- #80 partly defines the length of your pattern match. u_bits = and_bits(#80, stream) -- Create left most bit pattern. result = stream - u_bits -- remove from existing stream result *= 2 -- shift the stream u_bits /= #80 -- convert to right most bits. u_bits &= 0 if (u_bits[1]) then -- handle overflow bit result = 0 & result else u_bits = u_bits[2..length(u_bits)] end if result += u_bits -- reinsert adjusted bits into stream return result end function ------------------------- integer pattern, f, r sequence i_stream, t_stream, mask, matches pattern = 1 -- Build a test stream of bits to be scanned. i_stream = repeat(0, 255) -- {1, 2, 3, ... , 253, 254, 255} for A = 1 to 255 do i_stream[A] = A end for for A = 1 to 255 do r = rand(255) f = i_stream[A] i_stream[A] = i_stream[r] i_stream[r] = f end for ? i_stream t_stream = i_stream for A = 0 to 7 do -- You can use find() or match() instead of equality for longer patterns. -- If you use match, Your bit pattern can be a string instead of an integer. mask = (pattern = t_stream) -- because I used equality.. it is easier to list all matches not just the first. matches = {} f = find(1, mask) while (f) do matches &= f mask[f] = 0 f = find(1, mask) end while if (length(matches)) then -- I'm only displaying when a match occurs ? {A, matches} -- {shift_amount, matches} -- I'm telling how much I shifted to obtain those matches. end if t_stream = shift_stream(t_stream) end for machine_proc(26, 0) --wait_key() -----------------------------
----- Original Message ----- From: "Allen Robnett" <alrobnett at alumni.princeton.edu> To: <EUforum at topica.com> Sent: Tuesday, May 25, 2004 7:47 AM Subject: bit manipulation > > > My ignorance of programming is astounding, but I think I understand that > all information is stored and transferred in bits. What environment > would I need to use in order to have a program scan a series of > integers, looking for a specific bit pattern, without first having to > convert the integers to sequences of bits? I assume that a machine > language program would do this naturally. If so, what is the best way to > do this without loosing all of the convenience of Euphoria? > > Allen > > > >