Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #9222

Re: Simpler way to dp binary logical operation using arithmetic?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.programming
Subject Re: Simpler way to dp binary logical operation using arithmetic?
Date 2018-09-10 18:50 +0200
Organization A noiseless patient Spider
Message-ID <pn67cd$6jq$1@dont-email.me> (permalink)
References <bx7o6lmzra8t.mz0g7tl4wryj.dlg@40tude.net>

Show all headers | View raw


On 10/09/18 17:56, JJ wrote:
> I'm using a programming language which is pretty limited. FYI, it's part of
> an Android automation application. It has a custom syntax which is similar
> to Java.
> 
> I want to perform binary logical operations such as NOT, OR, and AND. While
> the language does support logical operators, they're only applicable for
> boolean value types. They're not applicable for integers. e.g. in a full
> featured programming language, below code is possible.
> 

You seem to be a little muddled here.  Let's rewind a little.

There are two sorts of AND/OR/NOT operations - logical (boolean) 
operations, and bitwise operations.  In C and Java, the logical 
operations are &&, || and !, while the bitwise ones are &, | and ~.  I 
will stick to those here, although there is no guarantee that your 
language has these.

Logical operations operate on boolean values.  In many languages, they 
are allowed for integers too - an integer value of 0 counts as "false" 
and non-zero counts as "true".  So you get:

	0x12 && 0x55		// true

Bitwise operations operate on integers (preferably unsigned integers), 
and are done a bit at a time.  So you get:

	0x12 & 0x55		// 0x10

If your language supports bitwise operations (suggested by your code 
below) but not logical operations, then the logical versions are easy to 
simulate.  To get "x & y" you use:

	(x != 0) & (y != 0)	// Same as "x && y"
	(x != 0) | (y != 0)	// Same as "x || y"
	(x == 0)		// Same as "!x"

If your language supports logical operations, but not bitwise 
operations, then it is harder to make the bitwise operations manually. 
Try something like this (structured for clarity and flexibility) :

uint8_t bitwise_and8(uint8_t x, uint8_t y) {
	uint8_t z = 0;
	for (int n = 0; n < 8; n++) {
		z = z * 2;

		bool bitX;
		bool bitY;
		if (x >= 0x80) {
			bitX = true;
			x -= 0x80;
		} else {
			bitX = false;
		}
		if (y >= 0x80) {
			bitY = true;
			y -= 0x80;
		} else {
			bitY = false;
		}
		if (bitX && bitY) {
			z += 1;
		}
		
		x = x / 2;
		y = y / 2;
	}
	return z;
}

That only uses simple operations found in most programming languages. 
If you need larger sizes, you can use a longer loop.  And the extensions 
to "or" and "xor" are obvious.





>      value1 = 0xA5;
>      value2 = 0x5A;
>      result = value1 | value2;
>      //result is 0xFF
> 
> However, in the Android application that I use, it cause an error saying
> that `value1` or `value2` can not be converted to boolean. Meaning that the
> `|` operator only works on boolean types.
> 
> The only thing I could think of to achieve this is to extract each bit one
> by one from each two values by using the modulo operator, manually perform
> the bitwise logical operation using `if` statement on each bit one by one,
> then pack the separated bit values back into one value. e.g.
> 
>      //extract all bits of the first value...
>      value1bit0 = value1 % 2;
>      value1reminder = value1 \ 2; //"\" operator is an integer division
>      value1bit1 = value1reminder % 2;
>      value1reminder = value1reminder \ 2;
>      //etc. up to 31th or 63th bit
> 
>      //extract all bits of the second value...
>      value2bit0 = value2 % 2;
>      value2reminder = value2 \ 2;
>      //etc.
> 
>      //perform a bitwise OR operation...
>      if (value1bit0 || value2bit0) {
>        resultbit0 = 1;
>      } else resultbit0 = 0;
>      if (value1bit1 || value2bit1) {
>        resultbit1 = 1;
>      } else resultbit1 = 0;
>      //etc. up to 31th or 63th bit...
>      //this can be simplified using an array,
>      //but simple code is used to make it simpler to understand
> 
>      //pack the bit values back to one value...
>      result = resultbit0 || (resultbit1 * 2) || (resultbit2 * 4) ||
>               (resultbit3 * 2) || (resultbit4 * 4) || (resultbit5 * 4) ||
>               //etc. up to 31th or 63th bit...
> 
> Aa you can see, the above code is pretty long.
> 
> So, my question is: is there a simpler way to do this? Preferably, without
> extracting/repacking the bits, and performing the bitwise logical operation
> one bit at a time.
> 

Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Simpler way to dp binary logical operation using arithmetic? JJ <jj4public@vfemail.net> - 2018-09-10 22:56 +0700
  Re: Simpler way to dp binary logical operation using arithmetic? Julio Di Egidio <julio@diegidio.name> - 2018-09-10 09:30 -0700
    Re: Simpler way to dp binary logical operation using arithmetic? JJ <jj4public@vfemail.net> - 2018-09-11 02:13 +0700
      Re: Simpler way to dp binary logical operation using arithmetic? Julio Di Egidio <julio@diegidio.name> - 2018-09-10 14:21 -0700
  Re: Simpler way to dp binary logical operation using arithmetic? "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2018-09-10 18:32 +0200
    Re: Simpler way to dp binary logical operation using arithmetic? JJ <jj4public@vfemail.net> - 2018-09-11 02:26 +0700
      Re: Simpler way to dp binary logical operation using arithmetic? "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2018-09-10 22:00 +0200
        Re: Simpler way to dp binary logical operation using arithmetic? JJ <jj4public@vfemail.net> - 2018-09-11 03:22 +0700
  Re: Simpler way to dp binary logical operation using arithmetic? David Brown <david.brown@hesbynett.no> - 2018-09-10 18:50 +0200
    Re: Simpler way to dp binary logical operation using arithmetic? JJ <jj4public@vfemail.net> - 2018-09-11 03:02 +0700
      Re: Simpler way to dp binary logical operation using arithmetic? nobody@example.org (Scott) - 2018-09-11 15:24 +0000
  Re: Simpler way to dp binary logical operation using arithmetic? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-09-10 18:59 +0100
    Re: Simpler way to dp binary logical operation using arithmetic? JJ <jj4public@vfemail.net> - 2018-09-11 03:08 +0700

csiph-web