tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Shifting 128 bits



On Jul 5, 2014, at 2:48 PM, Roy Marples <roy%marples.name@localhost> wrote:

> Hi List!
> 
> I'm struggling with bit shifting, trying to implement an RFC.
> 
> Basically there is code that deals with uint128_t - an IPv6 address.
> As I cannot realistically use that in dhcpcd I need something else. However, 
> this is very much out of my comfort zone and I'm not good at handling bits!
> 
> Here is the code
> 
> uint8_t   l = 69;
> uint128_t p = a_ipv6address;
> uint128_t i = p << l;
> 
> uint8_t *id = a_buffer;
> uint8_t d = a_length;
> while (d-- > 0) {
>       *id++ = i >> 120;
>       i <<= NBBY;
> }
> 
> Can anyone help me please?

treat as two uint64_t

union xxx {
    uint128_t u128;
    uint64_t u64[2];
} x;

x.u128 = ipv6 addr;

shift left:

x.u64[0] <<= l;
x.u64[0] |= x.u64[1] >> (64 - l);
x.u64[1] <<= l;

shift right:

x.u64[1] >>= l;
x.u64[1] |= x.u64[0] << (64 - l);
x.u64[0] >>= l;

---

newaddr = x.u128


Home | Main Index | Thread Index | Old Index