in reply to length of 1's

Your code expects your 0s and 1s to be space separated but you do not mention that fact in your post. Is it? If not @bin may be empty as well.