Bitwise confusion?

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Bitwise confusion?

joec
I'm running LUA v5.3.2. In the code below shouldn't the following if statements produce the same results?

-----code below here------

local tracemask = 0x00000040
local traceflgs = 0

val = traceflgs & tracemask

if (traceflgs & tracemask) then
    print("val=" .. val .. "\n")
else
    print("Branch not taken...\n")
end

if ((traceflgs & tracemask) > 0) then
    print("val=" .. val .. "\n")
else
    print("Branch not taken...\n")
end

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Soni "They/Them" L.
On 07/01/16 05:35 PM, [hidden email] wrote:

> I'm running LUA v5.3.2. In the code below shouldn't the following if
> statements produce the same results?
>
> -----code below here------
>
> local tracemask = 0x00000040
> local traceflgs = 0
>
> val = traceflgs & tracemask
>
> if (traceflgs & tracemask) then
>     print("val=" .. val .. "\n")
> else
>     print("Branch not taken...\n")
> end
>
> if ((traceflgs & tracemask) > 0) then
>     print("val=" .. val .. "\n")
> else
>     print("Branch not taken...\n")
> end
>
if 0 then
   print"0 is true"
else
   print "0 is false"
end

if 0 > 0 then
   print "0 > 0 is true"
else
   print "0 > 0 is false"
end

http://www.lua.org/manual/5.3/manual.html#2.1

"Both nil and false make a condition false; any other value makes it true."

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

RE: Bitwise confusion?

joec
In reply to this post by joec
Thanks for the clarification, didn't see it like that. 

I thought that when the result of the if expression evaluates to true it takes the if branch, but this appears to be the opposite from what you're saying?

-------- Original Message --------
Subject: Re: Bitwise confusion?
From: "Soni L." <[hidden email]>
Date: Thu, January 07, 2016 2:44 pm
To: Lua mailing list <[hidden email]>

On 07/01/16 05:35 PM, [hidden email] wrote:
> I'm running LUA v5.3.2. In the code below shouldn't the following if
> statements produce the same results?
>
> -----code below here------
>
> local tracemask = 0x00000040
> local traceflgs = 0
>
> val = traceflgs & tracemask
>
> if (traceflgs & tracemask) then
> print("val=" .. val .. "\n")
> else
> print("Branch not taken...\n")
> end
>
> if ((traceflgs & tracemask) > 0) then
> print("val=" .. val .. "\n")
> else
> print("Branch not taken...\n")
> end
>
if 0 then
print"0 is true"
else
print "0 is false"
end

if 0 > 0 then
print "0 > 0 is true"
else
print "0 > 0 is false"
end

http://www.lua.org/manual/5.3/manual.html#2.1

"Both nil and false make a condition false; any other value makes it true."

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Coda Highland
On Thu, Jan 7, 2016 at 12:29 PM,  <[hidden email]> wrote:
> Thanks for the clarification, didn't see it like that.
>
> I thought that when the result of the if expression evaluates to true it
> takes the if branch, but this appears to be the opposite from what you're
> saying?

No, that's exactly what it's saying. However, zero is NOT false in
Lua, unlike in C.

Your bitwise operation is returning a numeric zero, which is neither
the specific value false, nor the specific value nil. Therefore, the
conditional is considered true, therefore the first block is executed.

Basically, you should always use a conditional operator (!= 0 perhaps)
when using the result of bitwise ops in a conditional.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Enrico Colombini
On 07-Jan-16 21:32, Coda Highland wrote:
> However, zero is NOT false in Lua, unlike in C.

I just got reverse-bitten by this in Python :-)

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Ką Mykolas
In reply to this post by Coda Highland
In case of Lua:
~= 0

;)

On 1/7/16, Coda Highland <[hidden email]> wrote:
> Basically, you should always use a conditional operator (!= 0 perhaps)
> when using the result of bitwise ops in a conditional.

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Coda Highland
On Thu, Jan 7, 2016 at 1:19 PM, Ką Mykolas <[hidden email]> wrote:
> On 1/7/16, Coda Highland <[hidden email]> wrote:
>> Basically, you should always use a conditional operator (!= 0 perhaps)
>> when using the result of bitwise ops in a conditional.
>
> In case of Lua:
> ~= 0
>
> ;)
>

Oh right, yes yes, I haven't actually written Lua code in earnest in
about a year and a half because of a job change. ^^() One of those
things I always wanted to get back to but haven't had a chance.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Jonathan Goble
On Thu, Jan 7, 2016 at 1:19 PM, Ką Mykolas <[hidden email]> wrote:
> On 1/7/16, Coda Highland <[hidden email]> wrote:
>> Basically, you should always use a conditional operator (!= 0 perhaps)
>> when using the result of bitwise ops in a conditional.
>
> In case of Lua:
> ~= 0
>
> ;)
>

Side question for Roberto and Luiz: why was ~= chosen for "not equals"
instead of the more standard != in most languages? Having to remember
to use ~= instead of != is a bit annoying when I'm switching back and
forth between multiple languages for different projects.

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Daurnimator
On 8 January 2016 at 08:41, Jonathan Goble <[hidden email]> wrote:

> On Thu, Jan 7, 2016 at 1:19 PM, Ką Mykolas <[hidden email]> wrote:
>> On 1/7/16, Coda Highland <[hidden email]> wrote:
>>> Basically, you should always use a conditional operator (!= 0 perhaps)
>>> when using the result of bitwise ops in a conditional.
>>
>> In case of Lua:
>> ~= 0
>>
>> ;)
>>
>
> Side question for Roberto and Luiz: why was ~= chosen for "not equals"
> instead of the more standard != in most languages? Having to remember
> to use ~= instead of != is a bit annoying when I'm switching back and
> forth between multiple languages for different projects.
>

IIRC, at the time of lua's creation, the world hadn't agreed on != yet.
Other languages (e.g. MATLAB) also use '~='

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Jonathan Goble
On Thu, Jan 7, 2016 at 5:30 PM, Daurnimator <[hidden email]> wrote:
> On 8 January 2016 at 08:41, Jonathan Goble <[hidden email]> wrote:
>> Side question for Roberto and Luiz: why was ~= chosen for "not equals"
>> instead of the more standard != in most languages? Having to remember
>> to use ~= instead of != is a bit annoying when I'm switching back and
>> forth between multiple languages for different projects.
>>
>
> IIRC, at the time of lua's creation, the world hadn't agreed on != yet.
> Other languages (e.g. MATLAB) also use '~='

OK, fair enough. I may look into tweaking the parser to accept != as
well as ~= for my own use, though that depends on whether I can wrap
my brain around the parser.

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Luiz Henrique de Figueiredo
> OK, fair enough. I may look into tweaking the parser to accept != as
> well as ~= for my own use, though that depends on whether I can wrap
> my brain around the parser.

It's the lexer that needs to be hacked and it's pretty easy;
just add this in function llex near to "case '~'":

      case '!': {
        next(ls);
        if (check_next1(ls, '=')) return TK_NE;
        else return '!';
      }


Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Jonathan Goble
On Thu, Jan 7, 2016 at 8:13 PM, Luiz Henrique de Figueiredo
<[hidden email]> wrote:

>> OK, fair enough. I may look into tweaking the parser to accept != as
>> well as ~= for my own use, though that depends on whether I can wrap
>> my brain around the parser.
>
> It's the lexer that needs to be hacked and it's pretty easy;
> just add this in function llex near to "case '~'":
>
>       case '!': {
>         next(ls);
>         if (check_next1(ls, '=')) return TK_NE;
>         else return '!';
>       }

Thanks. :-)

I figured out that it was the lexer after examining some of the power
patches on the wiki, specifically one for Lua 5.1 that added this
along with bitwise operators. Looks like it will be easier than I
expected.

Now that I have the ability to compile on Windows, I'm going to
experiment a bit with tweaking Lua to fit my tastes. Eventually I want
to create my own programming language from scratch for fun, probably
implemented in Lua or Python (given my limited C experience), and
gaining a greater understanding of the Lua internals through tweaking
and reading code will aid me in that goal.

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Dirk Laurie-2
2016-01-08 3:57 GMT+02:00 Jonathan Goble <[hidden email]>:

> Eventually I want to create my own programming language from
> scratch for fun, probably implemented in Lua or Python (given my
> limited C experience), and gaining a greater understanding of the
> Lua internals through tweaking and reading code will aid me
> in that goal.

Just switch goal and method: creating one's own programming language
from scratch and implementing it in Lua (where the C API also counts as
Lua, after all it occupies over 30% of the manual) is a good way to gain
a greater understanding of the Lua internals.

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Jonathan Goble
On Fri, Jan 8, 2016 at 12:11 AM, Dirk Laurie <[hidden email]> wrote:

> 2016-01-08 3:57 GMT+02:00 Jonathan Goble <[hidden email]>:
>
>> Eventually I want to create my own programming language from
>> scratch for fun, probably implemented in Lua or Python (given my
>> limited C experience), and gaining a greater understanding of the
>> Lua internals through tweaking and reading code will aid me
>> in that goal.
>
> Just switch goal and method: creating one's own programming language
> from scratch and implementing it in Lua (where the C API also counts as
> Lua, after all it occupies over 30% of the manual) is a good way to gain
> a greater understanding of the Lua internals.

Well, what I mean is that I want to study how Lua lexes, parses, and
compiles source code. That kind of thing isn't exposed in the API or
standard library. I need to understand that, or at least the
essentials, before I can take a stab at creating a new language.

Reply | Threaded
Open this post in threaded view
|

Re: Bitwise confusion?

Tony Papadimitriou
In reply to this post by Luiz Henrique de Figueiredo
And for those who are more Pascal oriented (i.e., not equal is <> which I
personally find more logical than !=), the case '<' can be changed to:

case '<': {
  next(ls);
  if (check_next1(ls, '=')) return TK_LE;
  else if (check_next1(ls, '<')) return TK_SHL;
  else if (check_next1(ls, '>')) return TK_NE;
  else return '<';
}


-----Original Message-----
From: Luiz Henrique de Figueiredo
Sent: Friday, January 08, 2016 3:13 AM
To: Lua mailing list
Subject: Re: Bitwise confusion?

> OK, fair enough. I may look into tweaking the parser to accept != as
> well as ~= for my own use, though that depends on whether I can wrap
> my brain around the parser.

It's the lexer that needs to be hacked and it's pretty easy;
just add this in function llex near to "case '~'":

      case '!': {
        next(ls);
        if (check_next1(ls, '=')) return TK_NE;
        else return '!';
      }