In my workflow, I need to do binary operations quite often. I am wondering if there is a clean way of creating macro/elisp-function to return value of following HASH function.

  • Input 16bit hex number: NUM
  • Hash function return value = NUM[15] ^ NUM[13] ^ NUM[11] ^ ... ^ NUM[1] (here ^ means XOR)
  • For example:
    • If NUM(in hex)=0001, hash=0
    • if NUM(in hex)=8000, hash=1
    • if NUM(in hex)=000A, hash=0
  • This seems equivalent to ((NUM & 0x55) > 0)?1,0) in C syntax. i.e. check if any of the odd bits are 1. – Juancho Jun 7 '18 at 18:51
  • Hi Jiancho, not precisely. it's xor of all bits in odd position. Sorry, I'll edit the question to clarify. – StupidKris Jun 7 '18 at 18:55

In calc, you can perform this operation as follows:

  • type your number to check (e.g. 16#32FE)
  • keep odd numbered bits: 16#AAAA b a. This ands your number with 0xAAAA
  • binary unpack with b u. This returns a list of active bits.
  • count length of list with V #
  • check for parity: 2%

Turning this into a macro... (currently reading the manual).

  • Thanks Juancho, this worked perfectly as I had expected. So many things yet to be explored in calc <3 – StupidKris Jun 8 '18 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.