zsh-users
 help / color / mirror / code / Atom feed
* logical NOT
@ 2012-05-10 11:59 Mark van Dijk
  2012-05-10 12:30 ` René Neumann
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Mark van Dijk @ 2012-05-10 11:59 UTC (permalink / raw)
  To: zsh-users

Before I explain what I need let me explain why I need it.

Just now I was reading about MAC addresses, their binary
representation, and how MAC addresses are transmitted across a network.
This document mentions the address AC:DE:48:00:00:00. Converting that
to binary is easy:

for i (${(s.:.):-AC:DE:48:00:00:80}) echo $(( [#2] hex=0x$i ))

Here we can see 0xAC == 2#10101100.

From what I understand this is transmitted over a network from right to
left, so the receiver will receive 00110101 in that order. In this
sequence the first transmitted bit represents the individual/group
address bit and the second one represents the universally/locally
administered address bit, the reason for me reading this.

Then I wondered about how to let zsh "reverse" a binary number and came
up with the logical NOT: simply flip each bit. I've tried a lot of ways
but they all fail. What confuses me most is this:

% echo $(( [#2] ~0xAC ))
-2#10101101

I'd appreciate thoughts on this, and to hear if it makes any sense at
all :)

Thanks,
Mark.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: logical NOT
  2012-05-10 11:59 logical NOT Mark van Dijk
@ 2012-05-10 12:30 ` René Neumann
  2012-05-10 12:47 ` Jérémie Roquet
  2012-05-10 15:12 ` Bart Schaefer
  2 siblings, 0 replies; 7+ messages in thread
From: René Neumann @ 2012-05-10 12:30 UTC (permalink / raw)
  To: Mark van Dijk; +Cc: zsh-users

XOR with all ones to 'flip':

echo $(( [#2] 0xAC ^ 0xFF ))
2#1010011

But this is not the 'reversal' in the sense of changing the bit-order
(lsbf to msbf or vice versa).

For the simple reversal of the bits, well ... use rev :)

echo "2#"`echo $(( [##2] 0xAC )) | rev`
2#00110101

- René

Am 10.05.2012 13:59, schrieb Mark van Dijk:
> Before I explain what I need let me explain why I need it.
> 
> Just now I was reading about MAC addresses, their binary
> representation, and how MAC addresses are transmitted across a network.
> This document mentions the address AC:DE:48:00:00:00. Converting that
> to binary is easy:
> 
> for i (${(s.:.):-AC:DE:48:00:00:80}) echo $(( [#2] hex=0x$i ))
> 
> Here we can see 0xAC == 2#10101100.
> 
> From what I understand this is transmitted over a network from right to
> left, so the receiver will receive 00110101 in that order. In this
> sequence the first transmitted bit represents the individual/group
> address bit and the second one represents the universally/locally
> administered address bit, the reason for me reading this.
> 
> Then I wondered about how to let zsh "reverse" a binary number and came
> up with the logical NOT: simply flip each bit. I've tried a lot of ways
> but they all fail. What confuses me most is this:
> 
> % echo $(( [#2] ~0xAC ))
> -2#10101101
> 
> I'd appreciate thoughts on this, and to hear if it makes any sense at
> all :)
> 
> Thanks,
> Mark.
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: logical NOT
  2012-05-10 11:59 logical NOT Mark van Dijk
  2012-05-10 12:30 ` René Neumann
@ 2012-05-10 12:47 ` Jérémie Roquet
  2012-05-10 15:12 ` Bart Schaefer
  2 siblings, 0 replies; 7+ messages in thread
From: Jérémie Roquet @ 2012-05-10 12:47 UTC (permalink / raw)
  To: Mark van Dijk; +Cc: zsh-users

Hi,

2012/5/10 Mark van Dijk <lists+zsh@internecto.net>:
> % echo $(( [#2] ~0xAC ))
> -2#10101101
>
> I'd appreciate thoughts on this, and to hear if it makes any sense at
> all :)

16#0xAC = 2#10101100
~16#0xAC = ~2#00000000000000000000000010101100 (this is not “logical
not”, btw, it's “one's complement”)
~16#0xAC = 2#11111111111111111111111101010011
which is a *signed* negative number of value 2#10101101 (see
https://en.wikipedia.org/wiki/Two's_complement)
~16#0xAC = -2#10101101

Makes sense for me… the complement is not limited to the most
significant set bit and the bits on its right, and these are signed
numbers.

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: logical NOT
  2012-05-10 11:59 logical NOT Mark van Dijk
  2012-05-10 12:30 ` René Neumann
  2012-05-10 12:47 ` Jérémie Roquet
@ 2012-05-10 15:12 ` Bart Schaefer
  2012-05-10 18:02   ` Mikael Magnusson
  2 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2012-05-10 15:12 UTC (permalink / raw)
  To: zsh-users

On May 10,  1:59pm, Mark van Dijk wrote:
}
} Then I wondered about how to let zsh "reverse" a binary number and came
} up with the logical NOT: simply flip each bit.

As others have noted, that gives you the number what might be described
as "upside down", rather than backward.

mac2bin() {
  setopt localoptions extendedglob
  print ${(l:8::0:)${${(s.:.)1}//(#b)(*)/$(([#2]$match))}#2\#}
}

reverse() {
  setopt localoptions noksharrays
  local i a
  a=( ${(s::)*} )
  for i in {$#a..1}; print -n -- $a[$i]
  print
}

torch% mac2bin AC:DE:48:00:00:80
00000000 00000000 00110000 00000000 00000000 01010000
torch% reverse $(mac2bin AC:DE:48:00:00:80)
00001010 00000000 00000000 00001100 00000000 00000000


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: logical NOT
  2012-05-10 15:12 ` Bart Schaefer
@ 2012-05-10 18:02   ` Mikael Magnusson
  2012-05-12 11:41     ` Internecto List Subscriber
  2012-05-12 11:43     ` Mark
  0 siblings, 2 replies; 7+ messages in thread
From: Mikael Magnusson @ 2012-05-10 18:02 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

On 10/05/2012, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On May 10,  1:59pm, Mark van Dijk wrote:
> }
> } Then I wondered about how to let zsh "reverse" a binary number and came
> } up with the logical NOT: simply flip each bit.
>
> As others have noted, that gives you the number what might be described
> as "upside down", rather than backward.
>
> mac2bin() {
>   setopt localoptions extendedglob
>   print ${(l:8::0:)${${(s.:.)1}//(#b)(*)/$(([#2]$match))}#2\#}
> }
>
> reverse() {
>   setopt localoptions noksharrays
>   local i a
>   a=( ${(s::)*} )
>   for i in {$#a..1}; print -n -- $a[$i]
>   print
> }
>
> torch% mac2bin AC:DE:48:00:00:80
> 00000000 00000000 00110000 00000000 00000000 01010000
> torch% reverse $(mac2bin AC:DE:48:00:00:80)
> 00001010 00000000 00000000 00001100 00000000 00000000

Reverse can also be implemented more confusingly with Oa,
reverse() {
  setopt localoptions noksharrays
  print -r -- "${(j::)${(@Oa)${(s::)*}}}"
}
-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: logical NOT
  2012-05-10 18:02   ` Mikael Magnusson
@ 2012-05-12 11:41     ` Internecto List Subscriber
  2012-05-12 11:43     ` Mark
  1 sibling, 0 replies; 7+ messages in thread
From: Internecto List Subscriber @ 2012-05-12 11:41 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Bart Schaefer, zsh-users

Hello,

On Thu, 10 May 2012 20:02:46 +0200
Mikael Magnusson <mikachu@gmail.com> wrote:

> On 10/05/2012, Bart Schaefer <schaefer@brasslantern.com> wrote:
> > On May 10,  1:59pm, Mark van Dijk wrote:
> > }
> > } Then I wondered about how to let zsh "reverse" a binary number
> > and came } up with the logical NOT: simply flip each bit.
> >
> > As others have noted, that gives you the number what might be
> > described as "upside down", rather than backward.
> >
> > mac2bin() {
> >   setopt localoptions extendedglob
> >   print ${(l:8::0:)${${(s.:.)1}//(#b)(*)/$(([#2]$match))}#2\#}
> > }
> >
> > reverse() {
> >   setopt localoptions noksharrays
> >   local i a
> >   a=( ${(s::)*} )
> >   for i in {$#a..1}; print -n -- $a[$i]
> >   print
> > }
> >
> > torch% mac2bin AC:DE:48:00:00:80
> > 00000000 00000000 00110000 00000000 00000000 01010000
> > torch% reverse $(mac2bin AC:DE:48:00:00:80)
> > 00001010 00000000 00000000 00001100 00000000 00000000
> 
> Reverse can also be implemented more confusingly with Oa,
> reverse() {
>   setopt localoptions noksharrays
>   print -r -- "${(j::)${(@Oa)${(s::)*}}}"
> }

Thanks everyone for the useful examples and comments! :) 

Mark


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: logical NOT
  2012-05-10 18:02   ` Mikael Magnusson
  2012-05-12 11:41     ` Internecto List Subscriber
@ 2012-05-12 11:43     ` Mark
  1 sibling, 0 replies; 7+ messages in thread
From: Mark @ 2012-05-12 11:43 UTC (permalink / raw)
  To: zsh-users

Hello,

On Thu, 10 May 2012 20:02:46 +0200
Mikael Magnusson <mikachu@gmail.com> wrote:

> On 10/05/2012, Bart Schaefer <schaefer@brasslantern.com> wrote:
> > On May 10,  1:59pm, Mark van Dijk wrote:
> > }
> > } Then I wondered about how to let zsh "reverse" a binary number
> > and came } up with the logical NOT: simply flip each bit.
> >
> > As others have noted, that gives you the number what might be
> > described as "upside down", rather than backward.
> >
> > mac2bin() {
> >   setopt localoptions extendedglob
> >   print ${(l:8::0:)${${(s.:.)1}//(#b)(*)/$(([#2]$match))}#2\#}
> > }
> >
> > reverse() {
> >   setopt localoptions noksharrays
> >   local i a
> >   a=( ${(s::)*} )
> >   for i in {$#a..1}; print -n -- $a[$i]
> >   print
> > }
> >
> > torch% mac2bin AC:DE:48:00:00:80
> > 00000000 00000000 00110000 00000000 00000000 01010000
> > torch% reverse $(mac2bin AC:DE:48:00:00:80)
> > 00001010 00000000 00000000 00001100 00000000 00000000
> 
> Reverse can also be implemented more confusingly with Oa,
> reverse() {
>   setopt localoptions noksharrays
>   print -r -- "${(j::)${(@Oa)${(s::)*}}}"
> }

Thanks everyone for the useful examples and comments! :) 

Mark


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-05-12 11:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-10 11:59 logical NOT Mark van Dijk
2012-05-10 12:30 ` René Neumann
2012-05-10 12:47 ` Jérémie Roquet
2012-05-10 15:12 ` Bart Schaefer
2012-05-10 18:02   ` Mikael Magnusson
2012-05-12 11:41     ` Internecto List Subscriber
2012-05-12 11:43     ` Mark

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).