# translate bitwise function from C++

i have a C++ function i need to use in Xojo:

static inline uint8_t r_8(uint8_t value, int endian) {
if (endian) {
return (value << 8) | (value >> 8);
} else {
return value;
}
}

My best guess for Xojo is the following:

``````	Private Function r_8(value As UInt8, endian As Integer) As UInt8
if (endian)=1 then

return Bitwise.BitOr(Bitwise.ShiftLeft(value,8), Bitwise.ShiftRight(value,8))

else
return value

end if

End Function
``````

am i close. i’m not strong on bitwise functions.

any help appreciated.

It looks correct to me.

thanks.

would this in C++:

return (uint16_t)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8);

translate to this this in Xojo:
return CType(Bitwise.BitOr(Bitwise.ShiftLeft(Bitwise.BitAnd(value, 0xFFU), 8), Bitwise.ShiftRight(Bitwise.BitAnd(value, 0xFF00U),8)), UInt16)

I’m not sure. It looks correct, but my knowledge C++ is rather limited.

0xFFU is written in Xojo as &hFF. Likewse &hFF00.

sorry. got the hex number wrong

return CType(Bitwise.BitOr(Bitwise.ShiftLeft(Bitwise.BitAnd(value, &hFF), 8), Bitwise.ShiftRight(Bitwise.BitAnd(value, &hFF00),8)), UInt16)

This looks like a nibble swap gone wrong to me.

```static inline uint8_t r_8(uint8_t value, int endian) { if (endian) { return (value << 8) | (value >> 8); } else { return value; } }```

Value is an unsigned 8 bit integer. Shifting it left or right 8 bits zeros it.

It looks to me like C++ code was intended to swap the bytes in case the endian(ism) was incorrect.
but…
This function either returns 0 or the original value depending on endian(ism).

``````XXXYYYY << 8 = 00000000
XXXXYYYY >> 8 = 00000000
00000000 | 0000000 = 0``````

Makes no sense.

If the intent was a nibble swap I would have suggested:

``````XXXXYYYY << 4 = YYYY0000
XXXXYYYY >> 4 =  0000XXXX
then
YYYY0000 | 0000XXX = YYYYXXXX``````
``````static inline uint8_t r_8(uint8_t value, int endian) {
if (endian) {
return (value << 4) | (value >> 4);
} else {
return value;
}
}``````

And the nibbles in the byte are swapped.
I’ve never seen the need to swap nibbles as a function of endianism, but perhaps the OP has.

thanks. it is an endian swap. i’ll check the issues you’ve addressed.

Note that using Or, And, and Xor are faster than the BitWise equivalents.

[quote=36288:@Brian O’Brien]This looks like a nibble swap gone wrong to me.

Makes no sense.

I’ve never seen the need to swap nibbles as a function of endianism, but perhaps the OP has.[/quote]

Totally agree, it is completely weird (and wrong) for a uint8,

Very hard language is this?

As Brian already pointed out, the original C code is quite some nonsense.

It takes and returns a “uint8_t” type, which reduces the value to 8 bit. The proposed solutions here, though, deal with 16 bit values, which seem to make sense, but they’re far from result that the original C code would produce.

Effectively, if endian is non-zero, the C function always returns 0. So, I guess that whoever wrote this code never really tested it in a case where endian-swapping is necessary.

So, the correct Xojo version of this code would be:

```function r_8 (v as Integer, endian as Integer) as Integer if endian <> 0 then return 0 else return v and &hFF end end function```

Unless…
Maybe the C programmer found that the r_8 function didn’t work right, so he added this line to his code to “fix” it:

``#define uint8_t int``

I wouldn’t be the first time I’d see something like this

IMO, whoever wrote this code has very (too) little understanding of programming in C. Is this some hardware related code, by any chance?

Besides, the original code is plain C, nothing that requires C++.

[quote=36281:@Thomas Polson]My best guess for Xojo is the following:

if (endian)=1 then[/quote]
Careful there!
In C, any non-zero value means true!

Therefore, your “if endian = 1” is not correct, as the user of the C code might as well pass -1 for “endian” and it would still run into the “return (value << 8) | (value >> 8);” line.

thanks for the help