Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting decimal to binary

    Hello everyone

    I have a dataset with a variable where the value of each observation on that variable is given by a number. This could be any number: 23, 32345, 3123 etc.

    I need to convert these numbers into a binary number. I then want to take each of the digits in the binary sequences and turn them into new variables.

    More precisely i want to take the first digit of all the binary sequences and turn them into a new variable. And the same for the second digit, third digit etc.

    the first variable created in this way will than be a binary variable that indicates whether each observation had a 1 or 0 on the first digit in their binary sequence.

    Do you have any suggestions how to do this?

    Thank you,
    Jonathan

  • #2
    Any number? How about 3.1415?

    Assuming you mean a positive integer, rather than any number, it's easiest to work from back to front. Let's assume your numbers are all < 64, then here's some code to get you started to your objective.
    Code:
    clear
    input int number
    1
    2
    3
    4
    42
    63
    end
    assert number < 2^6 & number > 0
    generate temp = number
    forvalues i=0/5 {
        generate d_`i' = mod(temp,2) if temp>0
        replace temp = floor(temp/2)
        }
    drop temp
    Code:
    . list, clean
    
           number   d_0   d_1   d_2   d_3   d_4   d_5  
      1.        1     1     .     .     .     .     .  
      2.        2     0     1     .     .     .     .  
      3.        3     1     1     .     .     .     .  
      4.        4     0     0     1     .     .     .  
      5.       42     0     1     0     1     0     1  
      6.       63     1     1     1     1     1     1

    Comment


    • #3
      Yes, I am sorry. All the "numbers" are positive integers.

      Is it possibe to edit you command to make all binary sequences at least 8 numbers long? Say we convert 2 to binary = 10

      then i would like it to be = 00000010

      or if the integer was 24 = 11000 = 00011000
      Last edited by Jonathan Marin; 04 Jun 2017, 01:46.

      Comment


      • #4
        Also see inbase.

        Best
        Daniel

        Comment


        • #5
          See also the egen function base() in egenmore (SSC). This produces a string variable: the corresponding numeric variable with leading zeros display format is then standard steps away.

          Code:
          clear
          input int number
          1
          2
          3
          4
          42
          63
          end
          
          egen sbinary = base(number) 
          gen nbinary = real(sbinary) 
          format nbinary %08.0f 
          
          list, sep(0)  
          
              +-----------------------------+
               | number   sbinary    nbinary |
               |-----------------------------|
            1. |      1    000001   00000001 |
            2. |      2    000010   00000010 |
            3. |      3    000011   00000011 |
            4. |      4    000100   00000100 |
            5. |     42    101010   00101010 |
            6. |     63    111111   00111111 |
               +-----------------------------+

          Comment


          • #6
            Thank you, Nick.

            Very useful

            Comment

            Working...
            X