hexa numbers operations : masking, shifting

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

hexa numbers operations : masking, shifting

Ni Va
Hi,

Can I do hexa numbers operations like shifting and masking in vimscript ?

let Address = 0x00000103

in C:
Address & 0x000000FF should returns 0x00000003
Address >> 8         should returns 0x00000001
 
in Vimscript ?

Thank you
NiVa

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: hexa numbers operations : masking, shifting

Ni Va
Le dimanche 3 février 2019 12:03:40 UTC+1, [hidden email] a écrit :

> Hi,
>
> Can I do hexa numbers operations like shifting and masking in vimscript ?
>
> let Address = 0x00000103
>
> in C:
> Address & 0x000000FF should returns 0x00000003
> Address >> 8         should returns 0x00000001
>  
> in Vimscript ?
>
> Thank you
> NiVa
Is this can be equivalent of the above masking operation ?

echo printf('0x%08X',address)[-2:-1]

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: hexa numbers operations : masking, shifting

John Little-4
In reply to this post by Ni Va
On Monday, February 4, 2019 at 12:03:40 AM UTC+13, [hidden email] wrote:
>
> Can I do hexa numbers operations like shifting and masking in vimscript ?

See and(), or(), and xor()

:echo and(0x0103, 0x00ff)

For shifting, multiplying or dividing by 2 would work.

:echo 0x0103 / 256

Regards, John Little

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: hexa numbers operations : masking, shifting

Ni Va
Le lundi 4 février 2019 02:29:29 UTC+1, John Little a écrit :

> On Monday, February 4, 2019 at 12:03:40 AM UTC+13, [hidden email] wrote:
> >
> > Can I do hexa numbers operations like shifting and masking in vimscript ?
>
> See and(), or(), and xor()
>
> :echo and(0x0103, 0x00ff)
>
> For shifting, multiplying or dividing by 2 would work.
>
> :echo 0x0103 / 256
>
> Regards, John Little
Thank you John!

Shifting can be made multiplying or dividing by pow(x,2).

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: hexa numbers operations : masking, shifting

John Little-4
On Monday, February 4, 2019 at 11:47:50 PM UTC+13, [hidden email] wrote:

I'm sorry, I meant to type "a power of 2" in my answer, but my fingers didn't obey.

> Shifting can be made multiplying or dividing by pow(x,2).

(Assuming you meant pow(2,x).)

You'd want to be very careful with that, pow() returns a Float and the result will become a Float, which is not valid in a lot of vim script contexts, including the bit functions.  Float-ness can propagate unexpectedly in vim script.  I'd use a literal number if the shift is constant, f.ex. "x / 16" for "x >> 4".  If a variable shift is called for maybe use float2nr(pow(2, x)).

Regards, John Little

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: hexa numbers operations : masking, shifting

Ni Va
Le mercredi 6 février 2019 03:04:19 UTC+1, John Little a écrit :

> On Monday, February 4, 2019 at 11:47:50 PM UTC+13, [hidden email] wrote:
>
> I'm sorry, I meant to type "a power of 2" in my answer, but my fingers didn't obey.
>
> > Shifting can be made multiplying or dividing by pow(x,2).
>
> (Assuming you meant pow(2,x).)
>
> You'd want to be very careful with that, pow() returns a Float and the result will become a Float, which is not valid in a lot of vim script contexts, including the bit functions.  Float-ness can propagate unexpectedly in vim script.  I'd use a literal number if the shift is constant, f.ex. "x / 16" for "x >> 4".  If a variable shift is called for maybe use float2nr(pow(2, x)).
>
> Regards, John Little
Yes it's done. Thank you John.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.