Discussion:
Shifting 128 bits
(too old to reply)
Roy Marples
2014-07-05 21:48:04 UTC
Permalink
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?

Thanks

Roy

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Matt Thomas
2014-07-05 22:22:29 UTC
Permalink
Post by Roy Marples
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
--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Loading...