Wikifunctions:Type proposals/Byte
Status
The type Z80/Byte already exists, but currently the community is asked not to use it. In order to get it ready for usage, a few decisions need to be made. The Byte type currently has no converters, parsers, and renderers. Whereas parsers and renderers can be updated later, changing converters would be a painstaking process and should be thus avoided. So let’s decide together on the converters.
Summary
A Byte represents one of 256 possible values, often represented by the natural numbers 0 to 255. A Byte consists of eight bits, which each can be 0 or 1. It is frequently represented by two hexadecimal digits, i.e. from 00 to FF.
Uses
- Why should this exist?
A sequence of Bytes is usually used to represent binary data. Bytes are also traditionally the simplest data types computation is performed on.
- What kinds of functions would be created using this?
There are quite a few functions which operate on Byte, mixing Boolean operations over the Byte viewed as an array of eight bits (e.g. Bitwise and, Bitwise or, Bitwise negation, left shift, etc.) and viewed as a number (addition, subtraction, multiplication, modulo, etc.). Besides operations on Bytes, there could also be operations to transform into and from bytes, e.g. for encoding Unicode codepoints, numbers, etc., although most of these will require a list of bytes.
- What standard concepts, if any, does this align with?
Bytes are such a widely used standard in computing that it is rarely explicated. ISO/IEC 2382-1:1993 is the current standard that defines bytes.
Structure
A byte consists of one key of type Natural number. The number must be smaller than 256.
Example values
Here is a value for the byte representing the value 27, or, in hexadecimal 1B, or in binary, 00011001.
{
"type": "Byte",
"value": {
"type": "Natural number",
"value": "27"
}
}
|
{
"Z1K1": "Z80",
"Z80K1": {
"Z1K1": "Z13518",
"Z13518K1": "27"
}
}
|
Validator
The validator ensures that the value is smaller than 256.
Identity
Two Bytes are the same if the strings in their single key are the same.
Converting to code
Python
Python does not have a native type for a Byte. The most idiomatic conversion in Python is to convert a Wikifunctions Byte to a Python int. Python does have a native representation for an array of Bytes, which is the bytes object. Using the Python int for a single Byte is consistent with what the elements of a Python bytes object are, e.g. try in a Python3 console
> type(b'Wiki'[2])
<class 'int'>
JavaScript
JavaScript does not have a native type for a Byte. The most idiomatic conversion in JavaScript is to convert a Wikifunctions Byte to a JavaScript number. JavaScript does have a native representation for an array of Bytes, which is Uint8Array. Using the JavaScript number for a single Byte is consistent with what the elements of a JavaScript Uint8Array are, e.g. try in a node or browser console:
> typeof new Uint8Array(1)[0]
'number'
Display function
The default display function displays a byte as a string of length 2, displaying the hexadecimal value. Languages can overwrite this.
The community can change this after the type proposal has been accepted.
Read function
The read function accepts any of the following formats:
- Two hexadecimal digits, the letters either capitalized or not
- A three or one digit digital number
- An eight digit binary number
Note that a two digit digital number will be understood as the corresponding hexadecimal number, i.e. 20 will be read as “20” in hex, i.e. 32 in digital, not as 20 in digital, which would be 14 in hex. If one wants to enter a two digit digital number, they have to add a preceding zero, i.e. type 020.
Individual languages can overwrite the parser.
The community can change this after the type proposal has been accepted.
Alternatives
The current proposal is Option 3 in the following list of Alternatives.
- (Option 1) A byte is represented by one key of type string with two hexadecimal characters from 0-9 and A-F.
- (Option 2) A byte consists of one key that represents the value of the byte. The value is of type string, and has always exactly eight characters, each either 0 or 1.
- (Option 3) A byte consists of one key that represents the value of the byte. The value is of type natural number, and has to be between the values of 0 and 255, including the edges.
- (Option 4) A byte consists of eight keys, each of type Boolean.
- (Option 5) A byte consists of one key of type list of Booleans. The list has a length of exactly eight.
- (Option 6) we could represent the type as an enumeration of 256 values
- (Option 7) we could create a type for hexadecimal digit, and make this type have two keys of hexadecimal digit
Since Byte already exists, we should avoid trying to change the structure of Byte.
(Option 8) The renderer could also display the value as a string of eight 0s and 1s.
(Option 9) The renderer could also display a different set for the hexadecimal digits, but it seems that 0…9A…F is widely used also in languages that do not use the latin alphabet.
(Option A) The Python conversion could convert to a bytes object of length 1 instead of a value of type int.
(Option B) The JavaScript conversion could convert to a Uint8Array(1) object.
Comments
Support as proposer. --Denny (talk) 08:28, 8 February 2025 (UTC)
Oppose the use of hex here. It's not efficient, it's not language neutral, it's just all around meh. I am
Neutral to @99of9:'s proposal in the Telegram, I think it's still flawed, but the best I've seen proposed. Feeglgeef (talk) 00:58, 1 December 2024 (UTC)
Support no better option. Feeglgeef (talk) 15:51, 9 February 2025 (UTC)
- Just to record it here too, since it is difficult to find it in the chat: @99of9 was suggesting Option 3. I am OK with that and will rewrite the proposal accordingly. --Denny (talk) 08:22, 8 February 2025 (UTC)
- Updated. Supports would be appreciated! --Denny (talk) 08:30, 8 February 2025 (UTC)
Support Thanks. I think this is the best option I can see. Unfortunately ZObjects wrapping natural numbers wrapping strings still seem like a horribly inefficient way of representing a single byte! So I hope there will be a way to represent big file types like images without going via (inefficient) bytes. One small comment is that I think the reader accepting either 0 or 3 digit integers will be confusing, so I think it should just accept 3 digits, or 2 hex. --99of9 (talk) 08:46, 8 February 2025 (UTC)
- I'd just store it as ASCII Feeglgeef (talk) 15:51, 9 February 2025 (UTC)
- Is that option 8 for this proposal, or do you mean for larger file types? 99of9 (talk) 20:15, 9 February 2025 (UTC)