Newlisp simulates the uint32_t type of C

Source: Internet
Author: User

Bitwise operations are often involved in algorithms, and these bitwise operations require fixed-width integers, such as the following example:

void Tea(uint32_t* v, uint32_t* k) {    uint32_t v0 = v[0], v1 = v[1], sum = 0, i;           /* set up */    uint32_t delta = 0x9e3779b9;                     /* a key schedule constant */    uint32_t k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3];   /* cache key */    for (i=0; i < 32; i++) {                       /* basic cycle start */        sum += delta;        v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);        v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);    }                                              /* end cycle */    v[0] = v0; v[1] = v1;    cout << v0 << endl;    cout << v1 << endl;}

This is a C language implementation of the tea algorithm. We need the unitn32_t type.

However, newlisp generally only has one Integer type, and its value range is-9,223,372,036,854,775,808 and + 9,223,372,036,854,775,807.

This is a 64-bit signed integer. So how can we limit the integer to uint32_t?

I wrote a function:

(define (u4 v)  (first (unpack "lu" (pack "lu" v))))

Use pack to convert the integer V to the uint32 string, and then use unpack to convert the string to the uint32 integer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.