# Befunge Golfing Tips

Post tips for Befunge code golf here!

This is based on a type of question I used to see on CGCC, where people would post tips for golfing in various languages. They happened to be quite helpful, so I thought it'd be nice to bring them back.

Exploiting layout Make it t …

7mo ago

Golfing numbers Numbers above …

12mo ago

Bitwise operations (WIP) B …

12mo ago

## 3 answers

# Golfing numbers

Numbers above 15 and below zero take more than one byte to create. Here's a table of numbers up to 100 (Not including negatives yet)

## Table for '98

NOTE: For numbers that are also valid ASCII characters, it can be smaller to use `" "`

to insert them. This isn't included in the table, as you can easily work this out yourself. 0..31 and 127 a hex editor to insert this way.

Number | Solution |
---|---|

16 | `f1+` |

17 | `f2+` |

18 | `f3+` |

19 | `f4+` |

20 | `f5+` |

21 | `f6+` |

22 | `f7+` |

23 | `f8+` |

24 | `f9+` |

25 | `fa+` |

26 | `fb+` |

27 | `fc+` |

28 | `fd+` |

29 | `fe+` |

30 | `ff+` |

31 | `ff1++` |

32 | `48*` |

33 | `b3*` |

34 | `f2+2*` |

35 | `57*` |

36 | `66*` |

37 | `66*1+` |

38 | `f4+2*` |

39 | `d3*` |

40 | `a4*` |

41 | `a4*1+` |

42 | `67*` |

43 | `67*1+` |

44 | `b4*` |

45 | `59*` |

46 | `59*1+` |

47 | `59*2+` |

48 | `68*` |

49 | `77*` |

50 | `a5*` |

51 | `f2+3*` |

52 | `d4*` |

53 | `d4*1+` |

54 | `69*` |

55 | `b5*` |

56 | `78*` |

57 | `78*1+` |

58 | `78*2+` |

59 | `78*3+` |

60 | `a6*` |

61 | `a6*1+` |

62 | `a6*2+` |

63 | `79*` |

64 | `88*` |

65 | `d5*` |

66 | `6b*` |

67 | `6b*1+` |

68 | `6b*2+` |

69 | `6b*3+` |

70 | `a7*` |

71 | `a7*1+` |

72 | `89*` |

73 | `89*1+` |

74 | `89*2+` |

75 | `5f*` |

76 | `5f*1+` |

77 | `b7*` |

78 | `6d*` |

79 | `6d*1+` |

80 | `8a*` |

81 | `99*` |

82 | `99*1+` |

83 | `99*2+` |

84 | `7c*` |

85 | `7c*1+` |

86 | `7c*2+` |

87 | `7c*3+` |

88 | `8b*` |

89 | `8b*1+` |

90 | `9a*` |

91 | `7d*` |

92 | `7d*1+` |

93 | `7d*2+` |

94 | `7d*3+` |

95 | `7d*4+` |

96 | `c8*` |

97 | `c8*1+` |

98 | `e7*` |

99 | `b9*` |

100 | `aa*` |

# Exploiting layout

## Make it thin

Newlines and indentation are expensive, so remove them when possible. Some examples:

```
&v factorial
v< >
>:1-:| v* <
>$>\:|
>$.@
```

Can be golfed significantly by removing `|`

, which requires at least 3 lines

```
&>:1-:#v_$>\:#v_$.@
^ < ^* <
```

It could probably be golfed further by using unefunge-98 and the `j`

command to remove the second line.

#### 0 comment threads

# Bitwise operations

(WIP)

Befunge, rather pointedly, lacks bitwise ops. No shifts, no and, no or, no xor.

## Shifts

Shifts can be implemented as multiplication and division by powers of two. Shifts by arbitrary amounts can be done with successive multiplication/division by two. Rendered tricky by negatives, all shifts below are only functional with 31 bits and leave 32nd untouched.

### Shift by n

#### Left shift

```
v
v
>1-\2*v
^_v#:\<
$
v
v
```

#### Right shift

```
v
v
>1-\2/v
^_v#:\<
$
v
v
```

#### Bidirectional shift (negative is right)

Note: not particularly golfed.

```
v
v
>:0`#v_0-v
v < v <
>1-\2*v>1-\2\v
^_v#:\<^_v#:\<
v <
$
v
v
```

## 1 comment thread