@wizzwizz4 I don't think so. They can't be bytes because they don't have 8 bits. Bitwise operators normally work on ints and not much else. If you have a better suggestion then I'm listening, but otherwise ints it is.
– mpenMar 22 '16 at 20:22

2

@wizzwizz4 Because I don't want people to be able to skip a step. I don't want answers like "the first 5 bits of this byte contain the answer" -- the result should not contain any superfluous information, and it should be easily converted back to ASCII or some character mapping (a real-life use-case). Also, given the number of answers so far, it doesn't appear to be a problem.
– mpenMar 22 '16 at 21:40

23 Answers
23

Pyth, 18 17 bytes

z get input
+C1 prepend a 0x01 to prevent leading zeroes from disappearing
.B convert to binary string
t remove the leading 1 from ^^
.[ \0Q pad right with zeroes to multiple of second input
c Q get chunks/slices of length second input
iR2 map(x: int(x, 2))

@WashingtonGuedes I managed to golf another 9 bytes off my own golf of your solution, but it's still 129 bytes, sorry: "(s,n)=>(s.replace(/./g,x=>(256+x.charCodeAt()).toString(2).slice(1))+'0'.repeat(n-1)).match(eval(`/.{${n}}/g`)).map(x=>+`0b${x}`)".length
– NeilMar 22 '16 at 9:57

You sure this one runs? The ungolfed version is crashing Chrome.
– mpenMar 22 '16 at 16:20

@mpen The golfed version definitely runs on Firefox. The ungolfed version may have errors in it.
– NeilMar 22 '16 at 16:34

Aha! And so it does. I thought Chrome's JS engine was ahead FF's but I guess not.
– mpenMar 22 '16 at 20:26

Instead of str_pad($s,8,'0',STR_PAD_LEFT), you can use str_pad($s,8,0,0). You can remove the quotes on bindec and decbin to save 4 bytes. To save more, you can store array_map in a variable and pass it instead. Here you go: function f($b,$n){$M=array_map;return$M(bindec,$M(function($x)use($n){return str_pad($x,$n,0);},str_split($M('',array_map(function($s){return str_pad($s,8,0,0);},$M(decbin,$b))),5)));} (184 bytes).
– Ismael MiguelMar 22 '16 at 9:30

1. You can write 2b8T rather than 2b8 0 to save a byte (the variable T is preinitialized to 0) 2. Discarding the last chunk can be done with W< (the variable W is initialized to -1) or ); (take out the last element and discard it).
– Esolanging FruitApr 30 '17 at 20:38

[:#.-@[>\;@(_8:{."1#:@])
#:@] NB. Convert each number in `b` to bits
_8:{."1 NB. Take the last 8 items for each
NB. (padding with zeroes at the front)
;@ NB. Make a list of all the bits
-@[ NB. Negate `n`
NB. (\ gives non-overlapping infixes if [<0)
>\ NB. Get non-overlapping n-sized infixes
[:#. NB. Convert those back to decimal

Takes input b as a string delimited by '' or as an array of comma-separated values like [102, 48, 111], then n.

8 # push 8
&B # implicitly take input b, and use 2-element convert to binary
# to push a binary matrix of 8 bits
! # transpose, so each column represents an input
w # implicitly take input n and swap it with binary matrix to top of stack
e # reshape into n rows, padding with zeros at end
# this matrix will have each column as an n-bit integer
! # transpose, so each row is now the n-bit integer
XB # convert each row to decimal
# implicit output

Powershell 146 bytes

Take in the buffer and convert it to a char array and then an integer array. For each of those convert to binary, pad the entries with 0's where needed, and join as one large string. Split that string on n characters and drop the blanks that are created. Each element from the split is padded (only the last element really would need it) and converted back into an integer. Output is an array

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).