How to convert base 10 into base 2

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to convert base 10 into base 2

msherman
Dear list, I received the following email from a colleague and was
asked to help him out. Unfortunately I don't have a solution for him and
thought someone out on the Listserv might so here it is. thanks.
martin sherman


I'm in the midst of conducting my first experience sampling study. I
have lesbian, gay, and bisexual (LGB) college students carrying around

PDAs that are loaded with a survey that they take every time their
identity as LGB people becomes salient. Some of the items use a
checkbox
format where each box represents a response option. For example, if
participants indicate that the identity salient event involved an
interpersonal interaction, they are asked to indicate the sexual
orientation of the person(s) with whom they interacted: LGB,
heterosexual, or unknown sexual orientation.

The survey software loaded onto the PDAs delivers the results from
checkbox items in a somewhat inconvenient format. To understand the
format, say that a person responds to the sample item above by
checking

the boxes for "LGB" and "unknown sexual orientation." One way to
represent these results would be 101, which indicates that the first
and
third boxes were checked. Instead of providing the responses in this
format, the survey software treats "101" as a base 2 number and then
converts it into base 10. Since 101 in base 2 is the same as 5 in base

10, the software spits out "5." Obviously, this base 10 version of the

response is not very useful for analyses. Thus, I am wanting to
convert

the number into base 2. Do you know of any function or syntax that can

do this?
Reply | Threaded
Open this post in threaded view
|

Re: How to convert base 10 into base 2

Maguin, Eugene
Martin,

I can't try this out because I am in the middle of something else but spss
has a format called positive integer binary (PIB). I wonder if you can read
in your data set in the normal fashion and then change the write format of
the target variables to PIB. It may be that you will have to redefine the
standard format for the target variables to something like F1.0 before doing
the PIB format change. The PIB description is in the documentation under
variables in the universals section.

If that fails, then i think this should work. I'm going to assume that you
want your converted numbers out in F format and not A format. Let X be the
incoming data value and Y be the converted data value.

Numeric Y(N3.0).
Compute #a=trunc(x/4).
Compute #b=trunc((x-#a*4)/2).
Compute Y=#a*100+#b*10+(x-(#a*4+#b*2)).

If you have lots of these to do then you can set this up in a do repeat
structure.

Gene Maguin
Reply | Threaded
Open this post in threaded view
|

Re: How to convert base 10 into base 2

Richard Ristow
In reply to this post by msherman
At 03:32 PM 12/14/2006, Martin Sherman wrote:

>I have lesbian, gay, and bisexual (LGB) college students carrying
>around PDAs that are loaded with a survey that they take every time
>their identity as LGB people becomes salient. Some of the items use a
>checkbox format where each box represents a response option.
>
>The survey software loaded onto the PDAs delivers the results from
>checkbox items in a somewhat inconvenient format. To understand the
>format, say that a person responds to [a] sample item by checking the
>boxes for "LGB" and "unknown sexual orientation." One way to represent
>these results would be 101, which indicates that the first and third
>boxes were checked. Survey software treats "101" as a base 2 number
>and then converts it into base 10 [Interjection: no, into a number.
>Base is irrelevant at this point, believe it or not.]

>Since 101 in base 2 is the same as 5 in base 10, the software spits
>out "5."

Again, being a little pedantic but it matters: it doesn't, I'm pretty
sure, spit out '5' in base 10; it spits out the (abstract) number whose
decimal representation is 5.

>Obviously, this version of the response is not very useful for
>analyses. Thus, I am wanting to convert the number into base 2.

Gene's got it right. On the other hand, I don't recommend converting
into F format. If you convert 5 into a number whose decimal
representation is 101, then if you look at the decimal presentation
(NOT at the number itself) you have more or less what you want; but if
you ever use the number for computation, say,...

Here's a routine to convert numbers 0-63 into six binary digits. Since
it uses a LOOP, it may easily be extended to any number of digits. To
be 'cute', I've used lower-case 'o', rather than '0', to represent
binary digit 0.

Notice I say "number to binary" rather than "decimal to binary". You'll
see that nothing in the code make reference to decimal representation;
and, in fact, SPSS numbers are not represented in decimal, internally.

*  ............................................... .
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |15-DEC-2006 15:01:10       |
|-----------------------------|---------------------------|
INSTANCE RESPONSES

    01        11
    02        15
    03        11
    04        63
    05        17
    06        42
    07        42
    08        38
    09        59
    10        53
    11        60
    12        34
    13        55
    14        45
    15        24
    16        30
    17         0
    18        12
    19        37
    20        62

Number of cases read:  20    Number of cases listed:  20


*  This converts numbers representable with up to 5.
*  binary digits; i.e., 0-63. It may easily be     .
*  extended.                                       .

STRING  RespBnry   (A8) /* Binary representation of
                         /* "RESPONSES".
NUMERIC #BinPlce   (F2) /* N, for binary digit (2**n)
        /#BinDigt   (F1) /* Value (0/1) of binary digit
        /#Analyze   (F3) /* Remaining part of number   */.
STRING  #BinChar   (A1) /* Character rep. of #BinDigt */.

COMPUTE #Analyze = RESPONSES.
LOOP #BinPlce = 0 TO 5.
.  COMPUTE #BinDigt = MOD(#Analyze,2).
*  Binary digits:                .
*  1 is represented as '1'.      .
*  0 is represented as 'o'.      .
.  RECODE  #BinDigt
       (0    = 'o')
       (1    = '1')
       (ELSE = '?') INTO #BinChar.
.  COMPUTE SUBSTR(RespBnry,
                   LENGTH(RespBnry)-#BinPlce,1)
                  =#BinChar.
.  COMPUTE #Analyze
          =(#Analyze-#BinDigt)/2.
END LOOP.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |15-DEC-2006 15:01:12       |
|-----------------------------|---------------------------|
INSTANCE RESPONSES RespBnry

    01        11      oo1o11
    02        15      oo1111
    03        11      oo1o11
    04        63      111111
    05        17      o1ooo1
    06        42      1o1o1o
    07        42      1o1o1o
    08        38      1oo11o
    09        59      111o11
    10        53      11o1o1
    11        60      1111oo
    12        34      1ooo1o
    13        55      11o111
    14        45      1o11o1
    15        24      o11ooo
    16        30      o1111o
    17         0      oooooo
    18        12      oo11oo
    19        37      1oo1o1
    20        62      11111o

Number of cases read:  20    Number of cases listed:  20

===================
APPENDIX: TEST DATA
===================
*  Test data:  Random integers that are representable in .
*  six bits, i.e. 0 to 63.                               .
NEW FILE.
INPUT PROGRAM.
NUMERIC INSTANCE  (N2),
         RESPONSES (F3).
LOOP INSTANCE = 1 TO 20.
.  COMPUTE RESPONSES
         = TRUNC(RV.UNIFORM(0,64)).
.  END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.