bitwise NOT

4,657 views
Skip to first unread message

Jonathan Pittman

unread,
Sep 9, 2012, 12:30:07 PM9/9/12
to golang-nuts
As I understand it, the ~ is a bitwise NOT in C.  And, in reading the spec, I believe &^ is the Go equivalent.  With that in mind, how would I do the following in Go?

#define ASYNC_INTERNAL_FLAGS    (~((1U << ASYNCB_FIRST_KERNEL) - 1))

I can get as far as...

const (
        ASYNC_INTERNAL_FLAGS = (1 << ASYNCB_FIRST_KERNEL) - 1
)

but all of these cause gofmt to yell and go run produces compile errors.

const (
        ASYNC_INTERNAL_FLAGS = (&^((1 << ASYNCB_FIRST_KERNEL) - 1))
        ASYNC_INTERNAL_FLAGS = &^((1 << ASYNCB_FIRST_KERNEL) - 1)
        ASYNC_INTERNAL_FLAGS &^= ((1 << ASYNCB_FIRST_KERNEL) - 1)

)

What is the syntax I need for this?

Paul Hankin

unread,
Sep 9, 2012, 12:50:25 PM9/9/12
to golan...@googlegroups.com
^ is the unary bitwise complement operator. &^ is binary 'and not' operator.

Here, I think you want:

ASYNC_INTERNAL_FLAGS = ^uint32(1 << ASYNCB_FIRST_KERNEL - 1)

-- 
Paul

Jan Mercl

unread,
Sep 9, 2012, 12:51:42 PM9/9/12
to Jonathan Pittman, golang-nuts
'&^' is a binary operator: http://golang.org/ref/spec#Arithmetic_operators
The unary bitwise complement is '^' (same part of the spec near the
end of that section).

-j

Jonathan Pittman

unread,
Sep 9, 2012, 12:52:41 PM9/9/12
to Paul Hankin, golan...@googlegroups.com
Ah.  Okay.  I read...
&^   bit clear (and not)    integers
to mean "and NOT" not "AND NOT."  Thanks!

Job van der Zwan

unread,
Sep 10, 2012, 4:40:56 AM9/10/12
to golan...@googlegroups.com, Paul Hankin
Maybe it would be more clear if those operators were written in all caps in the comments.

Jan Mercl

unread,
Sep 10, 2012, 5:07:30 AM9/10/12
to Job van der Zwan, golan...@googlegroups.com, Paul Hankin
On Mon, Sep 10, 2012 at 10:40 AM, Job van der Zwan
<j.l.van...@gmail.com> wrote:
> Maybe it would be more clear if those operators were written in all caps in
> the comments.

I'm not sure. I'm now confused what are the supposed differences
between 'and not', 'and NOT' and 'AND NOT'?

-j

Patrick Mylund Nielsen

unread,
Sep 10, 2012, 5:22:20 AM9/10/12
to Job van der Zwan, golan...@googlegroups.com, Paul Hankin
It would; or if "bitwise" was prepended.

On Mon, Sep 10, 2012 at 10:40 AM, Job van der Zwan <j.l.van...@gmail.com> wrote:

Jonathan Pittman

unread,
Sep 10, 2012, 1:23:46 PM9/10/12
to Jan Mercl, Job van der Zwan, golan...@googlegroups.com, Paul Hankin
"and NOT" vs "AND NOT"

grammar vs. identifiers

Using all caps would distinguish these as logical/bitwise operators as opposed to components of English grammar in a phrase or sentence.

Jan Mercl

unread,
Sep 11, 2012, 12:50:03 AM9/11/12
to Jonathan Pittman, Paul Hankin, golan...@googlegroups.com, Job van der Zwan

On Sep 10, 2012 7:23 PM, "Jonathan Pittman" <jonathan.m...@gmail.com> wrote:
>
> "and NOT" vs "AND NOT"
>
> grammar vs. identifiers
>
> Using all caps would distinguish these as logical/bitwise operators as opposed to components of English grammar in a phrase or sentence.

I honestly thank you for the explanation but I'm too dumb to get it. The(se) operators have the  same semantics in an English sentence as they have in a programming language, so I fail to understand what the difference is (modulo lexical grammar).

Not your fault, but mine for sure, sorry.

-j

Dan Kortschak

unread,
Sep 11, 2012, 2:27:26 AM9/11/12
to Jan Mercl, Jonathan Pittman, Paul Hankin, golan...@googlegroups.com, Job van der Zwan
What I think he's trying to get at is the difference between:

1. AND NOT - an operation that does a&^b.
2. and NOT - an operation that does both a&^b, and ^a.

Job van der Zwan

unread,
Sep 11, 2012, 2:47:09 AM9/11/12
to golan...@googlegroups.com, Jan Mercl, Jonathan Pittman, Paul Hankin, Job van der Zwan
On Tuesday, 11 September 2012 08:27:34 UTC+2, kortschak wrote:
What I think he's trying to get at is the difference between:

1. AND NOT - an operation that does a&^b.
2. and NOT - an operation that does both a&^b, and ^a.

Exactly.

And of course, normally one should immediately realise the latter would be the most unlikely of these two possible interpretations, but imagine you're hacking away at 4 AM, don't even notice that there's two ways to interpret it, and too tired to notice the weirdness of the latter.

Jan Mercl

unread,
Sep 11, 2012, 3:29:23 AM9/11/12
to Dan Kortschak, Jonathan Pittman, Paul Hankin, golan...@googlegroups.com, Job van der Zwan
On Tue, Sep 11, 2012 at 8:27 AM, Dan Kortschak
<dan.ko...@adelaide.edu.au> wrote:
> What I think he's trying to get at is the difference between:
>
> 1. AND NOT - an operation that does a&^b.
> 2. and NOT - an operation that does both a&^b, and ^a.

Thanks! The RHSs are finally something I can understand even though
that interpretation (2.b) never came to my mind before ;-)

Sorry to all for me taking few days to wake up.


On Tue, Sep 11, 2012 at 8:47 AM, Job van der Zwan
<j.l.van...@gmail.com> wrote:
> And of course, normally one should immediately realise the latter would be
> the most unlikely of these two possible interpretations, but imagine you're
> hacking away at 4 AM, don't even notice that there's two ways to interpret
> it, and too tired to notice the weirdness of the latter.

True story ;-)

-j

Dan Kortschak

unread,
Sep 11, 2012, 4:15:55 AM9/11/12
to Jan Mercl, Jonathan Pittman, Paul Hankin, golan...@googlegroups.com, Job van der Zwan
It actually took me a few goes to get it, so I'm not surprised you were confused.

Jonathan Pittman

unread,
Sep 11, 2012, 10:19:42 AM9/11/12
to Dan Kortschak, Jan Mercl, Paul Hankin, golan...@googlegroups.com, Job van der Zwan
Yes.  Thank you, Dan, for clarifying that.
Reply all
Reply to author
Forward
0 new messages