Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Single digit Roman numeral

C, 59 byte h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;} Old version, 60 byte: h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&amp...

posted 1y ago by H_H‭  ·  edited 1y ago by H_H‭

Answer
#6: Post edited by user avatar H_H‭ · 2023-11-07T09:59:45Z (about 1 year ago)
  • ## C, 59 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • ### Old version, 60 byte:
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'M'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`. This has the nice side effect that it works work lower and upper case letters, since it ignores all bits >3.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
  • ## C, 59 byte
  • ```c
  • h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • ### Old version, 60 byte:
  • ```c
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'M'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`. This has the nice side effect that it works work lower and upper case letters, since it ignores all bits >3.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
#5: Post edited by user avatar H_H‭ · 2023-11-06T09:50:24Z (about 1 year ago)
  • ## C, 59 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • ### Old version, 60 byte:
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'M'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
  • ## C, 59 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • ### Old version, 60 byte:
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'M'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`. This has the nice side effect that it works work lower and upper case letters, since it ignores all bits >3.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
#4: Post edited by user avatar H_H‭ · 2023-10-25T16:17:42Z (about 1 year ago)
  • ## C, 59 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • ### Old version, 60 byte:
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'N'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
  • ## C, 59 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • ### Old version, 60 byte:
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'M'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
#3: Post edited by user avatar H_H‭ · 2023-10-25T16:16:13Z (about 1 year ago)
  • ## C, 60 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'N'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
  • ## C, 59 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • ### Old version, 60 byte:
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'N'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
#2: Post edited by user avatar H_H‭ · 2023-10-25T16:06:37Z (about 1 year ago)
  • ## C, 60 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'N'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.
  • ## C, 60 byte
  • ```
  • h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
  • ```
  • Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.
  • `n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.
  • A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'N'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.
  • Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a `n&16`, which is 1 byte longer).
#1: Initial revision by user avatar H_H‭ · 2023-10-25T15:51:15Z (about 1 year ago)
## C, 60 byte

```
h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}
```

Not very creative, there is probably a smaller version.

`n&4` separates `'V'`/`5`, `'L'`/`50`, `'D'`/`500` and `'M'`/`1000`, which have bit 2 set and end up in this part: `n&2?5:n&1?1000:n&8?50:500`, and `'I'`/`1`, `'X'`/`10`, `'C'`/`100` , which have bit 2 cleared and end up in this part: `n&2?100:n&1?1:10`.

A similar thing is done after that, of `'V', 'L', 'D', 'M'`, only `'V'` has bit 1 set, so if `n&2` is true, the value is 5. If not, we check bit 0 with `n&1` for `'N'` or 1000, bit 3 for `'L'` or `50` and if it is none of them it must be `'D'` or `500`. A similar thing is done for the numbers `100`, `1` and `10`.