Quantcast
Channel: Tips for golfing in C - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 67

Answer by S.S. Anne for Tips for golfing in C

$
0
0

Abuse two's complement

A lot of expressions can be changed by (ab)using two's complement. Take this for example:

getchar()+1

If you need higher precedence, you can use this:

-~getchar()

Or, if you're using it as the condition for a loop, this can be used:

~getchar()

Additionally, instead of subtracting one after another subtraction:

a+b-1

you can add the bitwise-NOT of the second operand:

a+~b

This applies similarly to multiplication by 2:

2*(a-b)-1

can be shortened to:

a-b+a+~b

Viewing all articles
Browse latest Browse all 67

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>