<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Is it possible to perform bitwise operations on values in the eval function? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232225#M68866</link>
    <description>&lt;P&gt;You could write  your command in Python. See &lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEU2"&gt;this&lt;/A&gt; and &lt;A href="https://answers.splunk.com/answers/301449/why-is-my-custom-search-command-not-producing-expe.html"&gt;this&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Alternatively, try the following query I've written as I was really curious on how to achieve this from Splunk. The logic should work fine if you have more than 2 numbers, simply use another appendcols and play with the bitwise variables. This works with numbers up to 2^20 - 1, simply increase all the mvrange parameters if you want it to be able to cope with bigger numbers.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;EDIT1: to use your naming convention.&lt;/STRONG&gt;&lt;BR /&gt;
&lt;STRONG&gt;EDIT2: also calculating Z back at the bottom&lt;/STRONG&gt;&lt;BR /&gt;
&lt;STRONG&gt;EDIT3: quick performance improvements and now you don't have to specify X and Y twice&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| fields - count
| eval x = 63

| eval power = mvrange(0,20)
| mvexpand power
| eval base2 = pow(2, power)
| where x &amp;gt;= base2
| eval mydiv = floor(x / base2)
| eval mybin = mydiv % 2
| eval binx = mybin

| appendcols [
   | stats count
   | fields - count
   | eval y = 2

   | eval power = mvrange(0,20)
   | mvexpand power
   | eval base2 = pow(2, power)
   | where y &amp;gt;= base2
   | eval mydiv = floor(y / base2)
   | eval mybin = mydiv % 2
   | eval biny = mybin
 ]
| fillnull
| eval bitwiseOr = round((binx + biny) / 2)
| eval bitwiseAnd = (binx * biny)
| eval zAnd = bitwiseAnd * base2
| eval zOr = bitwiseOr * base2
| stats max(x) as x, max(y) as y, list(binx) as binx, list(biny) as biny, list(bitwiseOr) as bitwiseOr, list(bitwiseAnd) as bitwiseAnd, sum(zAnd) as zAnd, sum(zOr) as zOr
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And this is the result (see picture below):&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/962i344F5A9F576B253B/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jan 2016 11:40:44 GMT</pubDate>
    <dc:creator>javiergn</dc:creator>
    <dc:date>2016-01-14T11:40:44Z</dc:date>
    <item>
      <title>Is it possible to perform bitwise operations on values in the eval function?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232224#M68865</link>
      <description>&lt;P&gt;In the following query I'm trying to get the logical AND of two numbers:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; * | head 1 | eval x=2 | eval y=2 | eval z = and(x, y)) | table x y z
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Ideally the result would be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;x y z
2 2 2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;because function "and" above would be similar to python's:&lt;BR /&gt;
LOGICAL AND: x &amp;amp; y&lt;/P&gt;

&lt;P&gt;So it's easy in pseudo code/query, but is there a way to actually do this, or a plugin that provides these operations?  I wasn't able to find it in the command reference.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2016 00:01:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232224#M68865</guid>
      <dc:creator>whisperstream</dc:creator>
      <dc:date>2016-01-14T00:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to perform bitwise operations on values in the eval function?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232225#M68866</link>
      <description>&lt;P&gt;You could write  your command in Python. See &lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEU2"&gt;this&lt;/A&gt; and &lt;A href="https://answers.splunk.com/answers/301449/why-is-my-custom-search-command-not-producing-expe.html"&gt;this&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Alternatively, try the following query I've written as I was really curious on how to achieve this from Splunk. The logic should work fine if you have more than 2 numbers, simply use another appendcols and play with the bitwise variables. This works with numbers up to 2^20 - 1, simply increase all the mvrange parameters if you want it to be able to cope with bigger numbers.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;EDIT1: to use your naming convention.&lt;/STRONG&gt;&lt;BR /&gt;
&lt;STRONG&gt;EDIT2: also calculating Z back at the bottom&lt;/STRONG&gt;&lt;BR /&gt;
&lt;STRONG&gt;EDIT3: quick performance improvements and now you don't have to specify X and Y twice&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| fields - count
| eval x = 63

| eval power = mvrange(0,20)
| mvexpand power
| eval base2 = pow(2, power)
| where x &amp;gt;= base2
| eval mydiv = floor(x / base2)
| eval mybin = mydiv % 2
| eval binx = mybin

| appendcols [
   | stats count
   | fields - count
   | eval y = 2

   | eval power = mvrange(0,20)
   | mvexpand power
   | eval base2 = pow(2, power)
   | where y &amp;gt;= base2
   | eval mydiv = floor(y / base2)
   | eval mybin = mydiv % 2
   | eval biny = mybin
 ]
| fillnull
| eval bitwiseOr = round((binx + biny) / 2)
| eval bitwiseAnd = (binx * biny)
| eval zAnd = bitwiseAnd * base2
| eval zOr = bitwiseOr * base2
| stats max(x) as x, max(y) as y, list(binx) as binx, list(biny) as biny, list(bitwiseOr) as bitwiseOr, list(bitwiseAnd) as bitwiseAnd, sum(zAnd) as zAnd, sum(zOr) as zOr
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And this is the result (see picture below):&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/962i344F5A9F576B253B/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2016 11:40:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232225#M68866</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-01-14T11:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to perform bitwise operations on values in the eval function?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232226#M68867</link>
      <description>&lt;P&gt;I have edited the above a couple of times:&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;EDIT1: to use your naming convention.&lt;BR /&gt;
EDIT2: also calculating Z back at the bottom&lt;BR /&gt;
EDIT3: quick performance improvements and now you don't have to specify X and Y twice&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2016 15:15:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232226#M68867</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-01-14T15:15:01Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to perform bitwise operations on values in the eval function?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232227#M68868</link>
      <description>&lt;P&gt;Example with 3 numbers (x, y, w):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| fields - count
| eval x = 19

| eval power = mvrange(0,21)
| mvexpand power
| eval base2 = pow(2, power)
| where x &amp;gt;= base2
| eval mydiv = floor(x / base2)
| eval mybin = mydiv % 2
| eval binx = mybin

| appendcols [
   | stats count
   | fields - count
   | eval y = 3

   | eval power = mvrange(0,20)
   | mvexpand power
   | eval base2 = pow(2, power)
   | where y &amp;gt;= base2
   | eval mydiv = floor(y / base2)
   | eval mybin = mydiv % 2
   | eval biny = mybin
]

| appendcols [
   | stats count
   | fields - count
   | eval w = 1

   | eval power = mvrange(0,20)
   | mvexpand power
   | eval base2 = pow(2, power)
   | where w &amp;gt;= base2
   | eval mydiv = floor(w / base2)
   | eval mybin = mydiv % 2
   | eval binw = mybin
 ]

| fillnull
| eval bitwiseOr = round((binx + biny) / 2)
| eval bitwiseOr = round((bitwiseOr + binw) / 2)
| eval bitwiseAnd = (binx * biny * binw)
| eval zAnd = bitwiseAnd * base2
| eval zOr = bitwiseOr * base2
| stats max(x) as x, max(y) as y, max(w) as w, list(binx) as binx, list(biny) as biny, list(binw) as binw, list(bitwiseOr) as bitwiseOr, list(bitwiseAnd) as bitwiseAnd, sum(zAnd) as zAnd, sum(zOr) as zOr
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jan 2016 15:49:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232227#M68868</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-01-14T15:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to perform bitwise operations on values in the eval function?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232228#M68869</link>
      <description>&lt;P&gt;Wow, thanks for this, amazing how much work it is for such a basic operation, hopefully someone from Splunk will take notice and add the features to the both eval and stats functions. &lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2016 16:04:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/232228#M68869</guid>
      <dc:creator>whisperstream</dc:creator>
      <dc:date>2016-01-14T16:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to perform bitwise operations on values in the eval function?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/674602#M230910</link>
      <description>&lt;P&gt;Now you can use the eval command's bitwise operators.&amp;nbsp;&lt;BR /&gt;&lt;A href="https://docs.splunk.com/Documentation/SplunkCloud/9.1.2308/SearchReference/BitFunctions" target="_blank" rel="noopener"&gt;https://docs.splunk.com/Documentation/SplunkCloud/9.1.2308/SearchReference/BitFunctions&lt;/A&gt;&lt;BR /&gt;Your query will look like:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;* | head 1 | eval x=2 | eval y=2 | eval z = bit_and(x, y) | table x y z&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2024 02:54:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-it-possible-to-perform-bitwise-operations-on-values-in-the/m-p/674602#M230910</guid>
      <dc:creator>jaypatel</dc:creator>
      <dc:date>2024-01-18T02:54:48Z</dc:date>
    </item>
  </channel>
</rss>

