Skip to content

Conversation

@jiribenes
Copy link
Contributor

Originally by @PhictionalOne, started in #1148 (was on a repo fork).

Resolves #814

Comment on lines +137 to +140
val unsigned: Int = b.toInt.bitwiseAnd(0xFF.toInt)

val high = unsigned.bitwiseShr(4).bitwiseAnd(0xFF.toInt)
val low = unsigned.bitwiseAnd(0x0F.toInt)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have bitwise operators on Bytes

Comment on lines +165 to +168
// NOTE: It seems like these cannot really be defined earlier... Oh well.

def show(b: Byte): String = b.toHexCharBytes.toString
def println(b: Byte): Unit = println(show(b))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only tested locally, needs a proper test.

Comment on lines 136 to 151
def toHexCharBytes(b: Byte): Unit / emit[Byte] = {
val unsigned: Int = b.toInt.bitwiseAnd(0xFF.toInt)

val high = unsigned.bitwiseShr(4).bitwiseAnd(0xFF.toInt)
val low = unsigned.bitwiseAnd(0x0F.toInt)
println(b.toInt.show ++ " (U" ++ unsigned.show ++ ") := " ++ high.show ++ "; " ++ low.show)

def toHexDigit(n: Int): Byte =
if (n < 10) {
(n + 48) // '0' = 48
} else {
(n - 10 + 65) // 'A' = 65
}.toByte

do emit(high.toHexDigit)
do emit(low.toHexDigit)
}
Copy link
Contributor Author

@jiribenes jiribenes Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The show instance for Bytes is a bit weird since we're trying to write it in Effekt directly.
In order to do anything at all, we need to either convert to characters or use bytearrays -- I've chosen to do the latter here, but as a consequence, the println and show for Byte are defined awkwardly in bytearray...

show(b: Byte) := bytearray::collect { b.toHexCharBytes }

js "'' + ${value}"
chez "(string ${value})"
llvm """
%z = call %Pos @c_bytearray_show_Byte(i8 ${value})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: now we should be able to deprecate c_bytearray_show_Byte 😌

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'll revert the custom show instance defined in Effekt directly and provide them via FFI instead.
Then we don't have to change the bytearray module.

@jiribenes jiribenes force-pushed the feat-byte-hex-notation branch from b959d97 to 5f45e95 Compare November 14, 2025 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add hexadecimal literals for Byte

3 participants