1
$\begingroup$

Maybe these are silly question. I know almost nothing about computer science, so please let me know in the comment section if I should edit anything or delete it.

  1. Let's say I have a function that randomly selects 8 choices of 0 or 1. Would it be written as one byte together? For example, if all choices came out as 1, would it be written as 11111111?

  2. Also, let's say another function can select the amount of choices. Any between 1 and 8 choices of either 0 or 1. As an example, if 3 choices were selected and all came out as 1, will it be written as 111? If so, how will the program know when to go to the next function in the case that every function had to select between 1 and 8 choices of 0 or 1?

$\endgroup$
6
  • $\begingroup$ What you describe can be represented by an 8-bit integer. Assuming it's non-negative, that integer would have a range between 0 and 2^8+2^7+...+2^0=511, to a total of 512 representable numbers. Any random number in that range would give you different combinations of zeros and ones $\endgroup$ Commented Mar 18, 2017 at 6:28
  • $\begingroup$ What I understand from the given description is that you have two function say f1 and f2. f1 generate 8 random bits. f2 select (not given how) a number between 1 to 8 and that many bits will be selected. In your example it is all 1s so you can select any three bits. But which three bits will be selected if there is some other bits. For function f1 you can generate an 8 bit number. $\endgroup$
    – Deep Joshi
    Commented Mar 18, 2017 at 6:38
  • $\begingroup$ nikaza, Thank you for taking your time. I don't understand. I thought 8 bits were 2^8, 256. What I wanna know is how it will look in binary if I had 3 choices, then 2, then 5 and so on, each being from a different function. $\endgroup$ Commented Mar 18, 2017 at 6:40
  • $\begingroup$ @Deep Joshi Thank you for commenting. I think I didn't explain it correctly. f1 (I think you got from question 1) was to see if I'm correct in thinking that it will be written as 8bits together. f2 (maybe you refer to question 2), this is the only type of function to be used. Let's say there are 4 functions that randomize 0 or 1 in any random amount. If they come up with amounts f1 (4), f2 (2), f3 (6) and f4 (3), will they be written as f1 (0110), f2 (11), f3 (011000) and f4 (110)? $\endgroup$ Commented Mar 18, 2017 at 7:00
  • $\begingroup$ @nikaza, Thank you for taking your time. I don't understand. I thought 8 bits were 2^8, 256. What I wanna know is how it will look in binary if I had 3 choices, then 2, then 5 and so on, each being from a different function. $\endgroup$ Commented Mar 18, 2017 at 7:02

1 Answer 1

1
$\begingroup$

In the binary system (much like in all numbering systems), order has meaning. If you write $0110$ this means $0\times2^3+1\times2^2+1\times2^1+0\times2^0$. Similarly, $11111111$ means $1\times2^7+1\times2^6+1\times2^5+1\times2^4+1\times2^3+1\times2^2+1\times2^1+1\times2^0=255$.

Now, assuming you had a function that produced a random bit as output, you would have 8 distinct bits if you called that function 8 times, which would form no kind of greater structure (that is, you would have e.g., 8 ones, but they would not mean 255 because they are not in an ordered structure, it's just 8 disjoint pieces of data).

On the other hand, if your function randomly selects the values of 8 bits and returns them, you would just have an array (or vector) of bits, which also do not represent a number.

In that sense, you can have the function output any number of bits you want. If you want two bits, you get 11 or 01 or 00 or 10, if you want three bits you get 111 or 000 or 100 or 010 or 001 or 110 or 101 or 011 etc. Note that in the way that you have phrased the question, these would not actually translate into numbers.

$\endgroup$
4
  • $\begingroup$ I believe you answered my question. 😀 Then, if I'm not mistaken, having random amounts anywhere from 1 to 8 for three functions of this sort will look like this: 010 111001 0011100. Right? $\endgroup$ Commented Mar 18, 2017 at 7:41
  • $\begingroup$ By any chance, do the spaces between them count as part of the file's size? $\endgroup$ Commented Mar 18, 2017 at 7:49
  • $\begingroup$ @Forrest Gump You can also output firstly the number of bits a function generated. From 1 to 8 will take 3 bits fixed length. More practically, you read 3 bits. Then parse and you know how many next bits represent the number. Read it. Then repeat again by reading 3 bits. Looks like this is a very space efficient storage. $\endgroup$
    – Eugene
    Commented Apr 17, 2017 at 8:32
  • $\begingroup$ "order has meaning" -- for binary, both orders are in use: MSB- and LSB-first (also called big and little endian). $\endgroup$
    – Raphael
    Commented Apr 17, 2017 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.