Statalist


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

Re: st: Return a specific bit from integer variable


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: Return a specific bit from integer variable
Date   Mon, 05 Oct 2009 10:57:38 -0400

The trick is to reduce by half going down from the top,
keeping the remainders:

 * generate an 8-bit integer *
 clear all
 set obs 1000
 gen myvar=round(uniform()*(2^7),1)

 * here's the part you need *
 clonevar newvar=myvar
 forv i=0/7 {
	local bit=7-`i'
	gen byte bit`i'=newvar-2*floor(newvar/2)
	replace newvar=floor(newvar/2)
 }


hth,
Jeph


Steinar Fossedal wrote:
Hello again, listers!

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?

Cheers,
-Steinar
*
*   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/

*
*   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/



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