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

Reverse the bits in a Byte

+2
−0

Simple challenge: Read a byte, swap bit 7 with 0, 6 with 1, 5 with 2 and 4 with 3 and then output the byte. For example the byte 0b1001'1100 would turn into 0b0011'1001.

Rules:

  • Shortest code wins
  • A byte has at least 8 bit. If it makes things easier, you can use a bigger byte size. The size should be independent of the value.
  • Reverse at least 1 byte. If it makes it easier, you can reverse more.
  • The byte to reverse is the only required input. But if it makes it easier, a input for the byte size and a input for the number of bytes to reverse can be required.
  • You can choose the input and output format, but both have to be the same.
  • If you choose a input format where multiple digits may needed, in a form which your language doesn't give a fixed size, for example a ASCII string or a list, then you have to support inputs that don't have leading 0s and inputs that do have leading 0s. For example, if you choose to use a binary number written in ASCII, the input maybe only "1010" (or "0b1010" if you prefer) and you would have to output either "01010000" or "1010000" (assuming you don't want to use digit separators nor a prefix and use 8 bit bytes).

Test cases

Input Byte -> Output Byte
0x00 -> 0x00
0x01 -> 0x80
0x02 -> 0x40
0x03 -> 0xc0
0x04 -> 0x20
0x05 -> 0xa0
0x06 -> 0x60
0x07 -> 0xe0
0x08 -> 0x10
0x09 -> 0x90
0x0a -> 0x50
0x0b -> 0xd0
0x0c -> 0x30
0x0d -> 0xb0
0x0e -> 0x70
0x0f -> 0xf0
0x10 -> 0x08
0x11 -> 0x88
0x12 -> 0x48
0x13 -> 0xc8
0x14 -> 0x28
0x15 -> 0xa8
0x16 -> 0x68
0x17 -> 0xe8
0x18 -> 0x18
0x19 -> 0x98
0x1a -> 0x58
0x1b -> 0xd8
0x1c -> 0x38
0x1d -> 0xb8
0x1e -> 0x78
0x1f -> 0xf8
0x20 -> 0x04
0x21 -> 0x84
0x22 -> 0x44
0x23 -> 0xc4
0x24 -> 0x24
0x25 -> 0xa4
0x26 -> 0x64
0x27 -> 0xe4
0x28 -> 0x14
0x29 -> 0x94
0x2a -> 0x54
0x2b -> 0xd4
0x2c -> 0x34
0x2d -> 0xb4
0x2e -> 0x74
0x2f -> 0xf4
0x30 -> 0x0c
0x31 -> 0x8c
0x32 -> 0x4c
0x33 -> 0xcc
0x34 -> 0x2c
0x35 -> 0xac
0x36 -> 0x6c
0x37 -> 0xec
0x38 -> 0x1c
0x39 -> 0x9c
0x3a -> 0x5c
0x3b -> 0xdc
0x3c -> 0x3c
0x3d -> 0xbc
0x3e -> 0x7c
0x3f -> 0xfc
0x40 -> 0x02
0x41 -> 0x82
0x42 -> 0x42
0x43 -> 0xc2
0x44 -> 0x22
0x45 -> 0xa2
0x46 -> 0x62
0x47 -> 0xe2
0x48 -> 0x12
0x49 -> 0x92
0x4a -> 0x52
0x4b -> 0xd2
0x4c -> 0x32
0x4d -> 0xb2
0x4e -> 0x72
0x4f -> 0xf2
0x50 -> 0x0a
0x51 -> 0x8a
0x52 -> 0x4a
0x53 -> 0xca
0x54 -> 0x2a
0x55 -> 0xaa
0x56 -> 0x6a
0x57 -> 0xea
0x58 -> 0x1a
0x59 -> 0x9a
0x5a -> 0x5a
0x5b -> 0xda
0x5c -> 0x3a
0x5d -> 0xba
0x5e -> 0x7a
0x5f -> 0xfa
0x60 -> 0x06
0x61 -> 0x86
0x62 -> 0x46
0x63 -> 0xc6
0x64 -> 0x26
0x65 -> 0xa6
0x66 -> 0x66
0x67 -> 0xe6
0x68 -> 0x16
0x69 -> 0x96
0x6a -> 0x56
0x6b -> 0xd6
0x6c -> 0x36
0x6d -> 0xb6
0x6e -> 0x76
0x6f -> 0xf6
0x70 -> 0x0e
0x71 -> 0x8e
0x72 -> 0x4e
0x73 -> 0xce
0x74 -> 0x2e
0x75 -> 0xae
0x76 -> 0x6e
0x77 -> 0xee
0x78 -> 0x1e
0x79 -> 0x9e
0x7a -> 0x5e
0x7b -> 0xde
0x7c -> 0x3e
0x7d -> 0xbe
0x7e -> 0x7e
0x7f -> 0xfe
0x80 -> 0x01
0x81 -> 0x81
0x82 -> 0x41
0x83 -> 0xc1
0x84 -> 0x21
0x85 -> 0xa1
0x86 -> 0x61
0x87 -> 0xe1
0x88 -> 0x11
0x89 -> 0x91
0x8a -> 0x51
0x8b -> 0xd1
0x8c -> 0x31
0x8d -> 0xb1
0x8e -> 0x71
0x8f -> 0xf1
0x90 -> 0x09
0x91 -> 0x89
0x92 -> 0x49
0x93 -> 0xc9
0x94 -> 0x29
0x95 -> 0xa9
0x96 -> 0x69
0x97 -> 0xe9
0x98 -> 0x19
0x99 -> 0x99
0x9a -> 0x59
0x9b -> 0xd9
0x9c -> 0x39
0x9d -> 0xb9
0x9e -> 0x79
0x9f -> 0xf9
0xa0 -> 0x05
0xa1 -> 0x85
0xa2 -> 0x45
0xa3 -> 0xc5
0xa4 -> 0x25
0xa5 -> 0xa5
0xa6 -> 0x65
0xa7 -> 0xe5
0xa8 -> 0x15
0xa9 -> 0x95
0xaa -> 0x55
0xab -> 0xd5
0xac -> 0x35
0xad -> 0xb5
0xae -> 0x75
0xaf -> 0xf5
0xb0 -> 0x0d
0xb1 -> 0x8d
0xb2 -> 0x4d
0xb3 -> 0xcd
0xb4 -> 0x2d
0xb5 -> 0xad
0xb6 -> 0x6d
0xb7 -> 0xed
0xb8 -> 0x1d
0xb9 -> 0x9d
0xba -> 0x5d
0xbb -> 0xdd
0xbc -> 0x3d
0xbd -> 0xbd
0xbe -> 0x7d
0xbf -> 0xfd
0xc0 -> 0x03
0xc1 -> 0x83
0xc2 -> 0x43
0xc3 -> 0xc3
0xc4 -> 0x23
0xc5 -> 0xa3
0xc6 -> 0x63
0xc7 -> 0xe3
0xc8 -> 0x13
0xc9 -> 0x93
0xca -> 0x53
0xcb -> 0xd3
0xcc -> 0x33
0xcd -> 0xb3
0xce -> 0x73
0xcf -> 0xf3
0xd0 -> 0x0b
0xd1 -> 0x8b
0xd2 -> 0x4b
0xd3 -> 0xcb
0xd4 -> 0x2b
0xd5 -> 0xab
0xd6 -> 0x6b
0xd7 -> 0xeb
0xd8 -> 0x1b
0xd9 -> 0x9b
0xda -> 0x5b
0xdb -> 0xdb
0xdc -> 0x3b
0xdd -> 0xbb
0xde -> 0x7b
0xdf -> 0xfb
0xe0 -> 0x07
0xe1 -> 0x87
0xe2 -> 0x47
0xe3 -> 0xc7
0xe4 -> 0x27
0xe5 -> 0xa7
0xe6 -> 0x67
0xe7 -> 0xe7
0xe8 -> 0x17
0xe9 -> 0x97
0xea -> 0x57
0xeb -> 0xd7
0xec -> 0x37
0xed -> 0xb7
0xee -> 0x77
0xef -> 0xf7
0xf0 -> 0x0f
0xf1 -> 0x8f
0xf2 -> 0x4f
0xf3 -> 0xcf
0xf4 -> 0x2f
0xf5 -> 0xaf
0xf6 -> 0x6f
0xf7 -> 0xef
0xf8 -> 0x1f
0xf9 -> 0x9f
0xfa -> 0x5f
0xfb -> 0xdf
0xfc -> 0x3f
0xfd -> 0xbf
0xfe -> 0x7f
0xff -> 0xff

Non-golfed Example in C

#include <stdio.h>
#include <stdint.h>

uint8_t reverseByte(uint8_t byte)
{
  uint8_t result=0;
  for(unsigned i=0; i<8; i++ )
    {
      result = (result<<1) | (byte&1);
      byte = byte>>1;
    } 
  return result;
}

int main(void)
{
  int c = getchar();
  if( c>= 0 )
    {
      putchar( reverseByte(c) );
    }
}

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Allowed output formats? (2 comments)

6 answers

+2
−0

Python (3.6 and up), 32 bytes (unsigned integer I/O)

lambda x:int(f'{x:08b}'[::-1],2)

Input and output are int objects. This also reverses the bits in integers larger than 255, implicitly inferring their bit length from the number of bits needed to write them (even if that isn't a multiple of 8). It will raise an exception if given a negative number as input.

Python (3.6 and up), 44 bytes (byte I/O)

lambda x:bytes([int(f'{x[0]:08b}'[::-1],2)])

Input and output are both actual bytes objects. This uses new-style string formatting to produce the bit-pattern directly (and takes advantage of the fact that indexing a bytes in 3.x gives an integer value), and uses the bytes constructor to produce a valid bytes object as the result (In Python 3.x, chr produces a string, not bytes).

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

ARM Thumb machine code, 6 bytes

0: fa90 f0a0  rbit  r0, r0 // reverse bits in 32-bit register 0
4: 4770       bx    lr     // return

if bytes have to be octets (8-bit):

ARM Thumb machine code, 8 bytes

0: fa90 f0a0  rbit  r0, r0      // reverse bits in 32-bit register 0
4: 0e00       lsrs  r0, r0, #24 // shift it right by 24 bits
6: 4770       bx    lr          // return
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Can't this be shortend? (3 comments)
+2
−0

Sclipting, (UTF-16) 26 bytes

갠긂갠밂乘감뒄뀢감雙갿및剩

Uses a formula I found here

unsigned char b; // reverse this (8-bit) byte
b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Is 341 shorter than 1023? (2 comments)
+1
−0

Python, 55 Bytes

lambda x:chr(int(bin(ord(x))[2:].rjust(8,"0")[::-1],2))

Takes input as the actual byte, and outputs the actual byte.

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

How do i input values above 127 and how values below 0x20? (2 comments)
+1
−0

C (gcc), 42 bytes

c;f(i){c++<8&&(printf("%d",i&1),f(i>>1));}

Try it online!

Binary output version.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

C (gcc), 47 bytes

r,c;f(i){return c++<8&&(r=r<<1|i&1,f(i>>1)),r;}

Try it online!

I didn't manage to come up with anything better than the most obvious recursive implementation posted above. I tried to use a different algorithm based on iterating two counters instead, but it got quite a bit longer...

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »