# String to Bit Transition

Given an input string of one or more ASCII characters which codepoints are between 0 and 128 (exclusive), do the following:

1. Convert each character into its 7-bit ASCII code (if the ASCII code is less than 7 bits, put leading zero bits)
2. Concatenate all bits (this results in 7*n bits where n is the number of characters)
3. For each bit in this bitstream, print 1 if it is different from the previous bit, and print 0 otherwise. The first output bit is always 1.

# Example

Input:

Hi

Output:

11011001011101

Explanation:

The string "Hi" has the ASCII codes

72 105

which in bits are:

1001000 1101001

And the transition bits indicators:

11011001011101

This is code golf. Lowest byte count wins.

# Test Cases

Test case 1:

Hello World!
110110010101110011010101101010110001110000111110000110000001011101101010101100110001

Test case 2:

%% COMMENT %%
1110111111011111100001100010010100001010110101011010011101010011111110011000001101111110111

Test case 3 (credit to Luis Mendo):

##
11100101110010

Congrats to Luis Mendo for the shortest solution with 9 bytes in MATL!

• Suggested test case ## (leading 0 bit; some answers currently fail because of that) Jul 10, 2019 at 9:25
• How is this a duplicate of the Manchester encoding challenge? Am I missing something? Jul 10, 2019 at 10:25
• The other challenge says converting an input stream of bits into a double-rate output stream, with each input '1' translated to a '01' and each input '0' translated to a '10'. So not dupe in my opinion. If a large number of people upvote @gastropner's comment above I can un-dupe (or any other user with that ability) Jul 10, 2019 at 11:24
• @Shaggy: Both test cases include a space, which has only a single bit set, and not the 7th. So I don't think the problem statement is guaranteeing that each ascii code will be exactly 7 bits in length. Jul 10, 2019 at 15:05
• @SmileAndNod On second thought, I think you don't need to handle empty string. Jul 17, 2019 at 21:47

# Python 2, 58 bytes

n=1
for c in input():n=n<<7|ord(c)
print'1'+bin(n^n/2)[4:]

Try it online!

• How big can n be that Python supports for this operation? Mar 25, 2021 at 6:12
• @justhalf I don't think there's any limit other than your computer's memory. Python supports whole numbers of arbitrary size, and bit operations don't assume any fixed size.
– xnor
Mar 25, 2021 at 6:20

# MATL, 9 bytes

Hj7&B!hdg

Try it online!

### Explanation

H     % Push 2
j     % Read line of input, unevaluated
7&B   % Convert to binary with 7 bits. Gives a 7-column matrix
!     % Transpose
h     % Concatenate horiontally. The matrix is read in column-major order
d     % Consecutive differences
g     % Convert to logical. Implicitly display
• This is the shortest so far. +1. It's fun to have a built-in for consecutive differences. Jul 12, 2019 at 5:19

# Japt-P, 11 bytes

Takes advantage of the fact that spaces can be coerced to 0 in JavaScript when trying to perform a mathematical or, in this case, bitwise operation on it.

c_¤ù7Ãä^ i1
c_¤ù7Ãä^ i1     :Implicit input of string
c_              :Map codepoints
¤             :  Convert to binary string
ù7           :  Left pad with spaces to length 7
Ã          :End map
ä^        :XOR consecutive pairs
i1     :Prepend 1
:Implicitly join and output
• The 7-bit means that if it is 32 (for space character), it would be 0100000. Also the % character (37) would be 0100101 Jul 10, 2019 at 16:58
• It's working now. +1 Jul 10, 2019 at 17:04

O+Ø⁷BḊ€FIA1;

Try it online!

# Octave, 36 30 bytes

Fix thanks to Luis Mendo

-2 bytes thanks to Sanchises

@(a)[1;~~diff(de2bi(a,7)'(:))]

Try it online!

• You can probably shave off quite a few bytes with de2bi. Jul 13, 2019 at 12:18
• Wasn't working for me before @sanchises but I'll take another look when I can Jul 13, 2019 at 12:23
• Try it online! Jul 13, 2019 at 15:08

# CJam, 21 bytes

1q{i2b7Te[}%e__(;.^);

Try it online!

## Explanation

Showing the stack with a sample input of 5:

1 q      e# Push 1 and then the whole input: 1 "5"
{
i      e# Convert to its char code: 1 [53]
2 b    e# Convert to binary: 1 [[1 1 0 1 0 1]]
7 T e[ e# Left-pad with 0 to length 7: 1 [[0 1 1 0 1 0 1]]
} %      e# Map this block over every character in the string
e_       e# Flatten array: 1 [0 1 1 0 1 0 1]
_ ( ;    e# Duplicate array and remove its first element: 1 [0 1 1 0 1 0 1] [1 1 0 1 0 1]
. ^      e# Element-wise xor: 1 [1 0 1 1 1 1 1]
) ;      e# Remove and pop the last element of the array: 1 [1 0 1 1 1 1]
e# Stack implicitly printed: 1101111

To see if a bit is different from the previous bit, we do a vector (element-wise) xor between the bit array and the bit array without the first element. We also remove the last bit of the result, because it is always the last bit of the longer array unchanged.

# APL (Dyalog Unicode), 16 bytesSBCS

Full program. Prompts for string from stdin.

1,2≠/∊1↓¨11⎕DR¨⍞

Try it online!

prompt for input ("a quote in a console")

11⎕DR¨ change each character to bit-Boolean Data Representation

1↓¨ drop the first bit from each

ϵnlist (flatten)

2≠/ pairwise difference

1, prepend a one

# Dart, 213 168 bytes

Previous one-liner

Try it online!

This verbosity and lack of easy built ins is really killing this one. Still managed to pull a one liner though.

• -45 bytes by not using a one liner and using a for loop

# Charcoal, 25 bytes

⭆θ◧⍘℅ι²¦⁷←Ｗⅈ←Ｉ﹪⍘ＫＤ²←01 ²1

Try it online! Link is to verbose version of code. Explanation:

⭆θ◧⍘℅ι²¦⁷←

Convert all the characters to binary and pad them to a length of 7 and then print them, but leave the cursor over the last digit.

Ｗⅈ

Repeat until the cursor is over the first digit.

←Ｉ﹪⍘ＫＤ²←01 ²

Calculate whether the digits are different and overwrite each digit with the difference.

1

Overwrite the first digit with a 1.

$args|%{$b=+$_ 6..0}|%{+($c-ne($c=($b-shr$_)%2))} Try it online! -17 bytes thanks to mazzy :) • Jul 10, 2019 at 15:52 • Awesome, you have to post it yourself. Jul 10, 2019 at 16:50 • this answer for you. my princess is in another castle :)))) Jul 10, 2019 at 16:56 • @mazzy, some more -7 bytes :) Jul 10, 2019 at 23:08 • awesome and brilliant! 【ツ】 Jul 11, 2019 at 4:41 # Ruby-p, 68 57 bytes -11 bytes by shamelessly stealing the method used by xnor's Python solution. l=1 gsub(/./){l=l<<7|$&.ord}
$_=?1+(l^l/2).to_s(2)[2..-1] Try it online! Original solution: gsub(/./){'%07b'%$&.ord}
l=p
gsub(/./){b=$&.ord-48;r=l ?l^b:1;l=b;r} Try it online! # 05AB1E (legacy), 12 bytes Çb7jð0:¥ÄJ1ì Uses the legacy version of 05AB1E, since j implicitly joins the strings together, which requires an explicit J after the j in the new version of 05AB1E. Explanation: Ç # Convert the (implicit) input-string to a list of ASCII code-points # i.e. "Hi#" → [72,105,35] b # Convert each integer to a binary string # → ["1001000","1101001","100011"] 7j # Prepend each with spaces to make them length 7, # and join everything together to a single string implicitly # → "10010001101001 100011" ð0: # Replace all those spaces with 0s # → "100100011010010100011" ¥ # Get the deltas of each pair of 1s/0s # → [-1,0,1,-1,0,0,1,0,-1,1,-1,0,1,-1,1,-1,0,0,1,0] Ä # Get the absolute value of this # → [1,0,1,1,0,0,1,0,1,1,1,0,1,1,1,1,0,0,1,0] J # Join them all together # → "10110010111011110010" 1ì # And prepend a 1 # → "110110010111011110010" # (after which the result is output implicitly) # Husk, 1615 14 bytes :1Ẋ≠ṁȯΩo=7LΘḋc Try it online! • It is really ridiculous how many bytes one seems to need to waste to pad with zeros to the left! I came up with this :1Ẋ≠ṁö↑_7+ḋ64ḋc as an alternative 15-byte solution... surely there's a slicker way...? Oct 16, 2020 at 12:55 # Vyxal, 20 16 15 bytes bƛṅS7↳vċ†;f¯ȧ1p Try it Online! A mess. -4 thanks to Aaron Miller. -1 thanks to lyxal. • 16 bytes Aug 2, 2021 at 20:53 • 15 bytes Aug 3, 2021 at 6:52 # Python 2, 104 bytes lambda w:reduce(lambda(A,P),C:(A+'10'[P==C],C),bin(reduce(lambda a,c:a*128+ord(c),w,1))[3:],('','x'))[0] Try it online! A quick stab at it. • Clever trick with a*128+ord(c)! But isn't the reduce and lambda kind of costly? Jul 10, 2019 at 3:54 # Stax, 13 12 bytes ìEÖâU₧(~¬8IE Run and debug it If it's guaranteed that all input characters have the 7th bit set, as some answers assume, it can be done in 10 bytes # Kotlin, 182 bytes var l='6' fun f(b:String)=b.fold(""){t,i->t+"".a(i.toInt())}.map{if(l==it){l=it;0} else {l=it;1}} fun String.a(v:Int):String=if(v<=0)"${this}0".reversed() else "${this}${v%2}".a(v/2)

Try it online!

Hopefully I can improve this soon, I feel like there must be some spots for improvement but I can't think right now

s/./sprintf'%07b',ord$&/ge;s/.(?=(.))/0|$&^$1/ge;s/^/1/;chop Try it online! # C (gcc (MinGW)), 90 bytes Requires a compiler providing itoa(). n[9],b,c;f(char*s){for(b=*s<64;c=*s++;printf("%07s",itoa((c^c/2)&127,n,2)))c|=b<<7,b=c&1;} • 88 bytes Sep 13, 2019 at 22:28 # Ruby-p, 50 bytes gsub(/./){"%07b"%$&.ord}
gsub(/./){$=~/#$&$/?0:1} Try it online! ## Explanation First line, same as Value Ink's answer: gsub(/./){$&    }   # Replace each character $&… .ord # …with its ASCII code… % # …formatted as… "%07b" # …binary digits padded to 7 places. Second line: gsub(/./){$&      }  # Replace each character $&…$               # …if the text to its left…
=~             # …matches…
/#  $/ # …the Regexp /c$/ where "c" is the character…
?0:1   # …with 0, or 1 otherwise.

Try it online!

# C# (Visual C# Interactive Compiler), 80 bytes

s=>{for(int p,c=2,i=0;i<7*s.Length;Write(p==c?0:1))(p,c)=(c,1&s[i/7]>>6-i++%7);}

Try it online!

# JavaScript (V8), 73 bytes

s=>[...c=s.repeat(7)].map((_,i)=>(c=1&s.charCodeAt(i/7,p=c)>>6-i%7)!=p|0)

Try it online!