[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: re: return a specific bit from integer variable |

Date |
Mon, 5 Oct 2009 12:47:52 +0100 |

See also 1. official command -inbase-. 2. -egen- function -base()- in -egenmore- from SSC. The help gives an example that seems parallel to Steinar's problem. Note that -split- (as suggested by Martin Weiss) is no help here. -split- is for strings subdivided by parsing or punctuation characters. Nick n.j.cox@durham.ac.uk Kit Baum Given an integer between 0-255 and the desired bit to be retrieved (counting from the left or high-order bit), this routine will give you the binary form of the integer and the bit as locals: mata: mata clear mata: void bit(real scalar v, real scalar w) { string vector d, b d=("0000","0001","0010","0011","0100","0101","0110","0111", // "1000","1001","1010","1011","1100","1101","1110","1111") for(i=1;i<=cols(d);i++) { dd = d[i] :+ d b = b, dd } if( v>0 & v <= 255 & w > 0 & w <= 8) { st_local("v" , b[v+1] ) st_local("w", substr(b[v+1], w, 1)) } } end . mata: bit(128,1) . di "`v' `w'" 10000000 1 . mata: bit(128,3) . di "`v' `w'" 10000000 0 Steinar Fossedal ================ I have imported an 8-bit integer variable where each bit represents a flag telling me if some property of the observation is true or false. E.g. the number 8 in binary format is 00001000, while 10 would be 00001010. Bit nr. 4 from the left tells me if a car is delivered or not, while the second to the right tells me if it's paid or not. So 8 = car is delivered but not paid, 2 = paid but not delivered, 10 = paid and delivered, etc. I need to split this information into 8 dummyvariables so they can be used to segment my data. Does there exist a fairly smooth way to do this in Stata? * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: RE: re: return a specific bit from integer variable***From:*Austin Nichols <austinnichols@gmail.com>

**References**:**st: re: return a specific bit from integer variable***From:*Kit Baum <baum@bc.edu>

- Prev by Date:
**st: create new variable from imputed variable; -ice-** - Next by Date:
**st: RE: RE: nested forvalues loop** - Previous by thread:
**st: re: return a specific bit from integer variable** - Next by thread:
**Re: st: RE: re: return a specific bit from integer variable** - Index(es):

© Copyright 1996–2017 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |