Re: Operating with binary numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg59068] Re: [mg59048] Operating with binary numbers
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Thu, 28 Jul 2005 02:26:16 -0400 (EDT)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
bitOnQ[x_Integer, pos_Integer]:= IntegerDigits[x,2][[-pos]]==1/;x>=2^(Abs[pos]-1); bitOnQ[x_Integer, pos_Integer]:= False/;x<2^(Abs[pos]-1); binary=2^^110110101010111010; m=Length[IntegerDigits[binary,2]]; bitOnQ[binary,4] True Table[bitOnQ[binary,-n],{n,m}] {True,True,False,True,True,False,True,False,True,False,True,False,True,True,\ True,False,True,False} binary==FromDigits[%/.{True->1,False->0},2] True Table[bitOnQ[binary,n],{n,m}]== Reverse[Table[bitOnQ[binary,-n],{n,m}]] True To shift, use IntegerDigits and either drop bits or add them IntegerDigits[binary,2] {1,1,0,1,1,0,1,0,1,0,1,0,1,1,1,0,1,0} Bob Hanlon > > From: "Wishmaster" <wishmaster at imaginary.marreka.no-ip.com> To: mathgroup at smc.vnet.net > Date: 2005/07/27 Wed AM 01:26:13 EDT > Subject: [mg59068] [mg59048] Operating with binary numbers > > Hello, > > I found that you can represent binary numbers in Mathematica with this: > > binary = 2^^110110101010111010 > 223930 > > And BaseForm[binary, 10] returns the number in decimal form > 223930 > > I'd like to know if is there some function to test if a given bit of the > binary number is on, and to do operations like "shifting" the binary number. > I looked at the help but can't find anything. > > I use Mathematica 5.0 on Windows XP. > > Thanks. > -- > []s Renan (aka Wishmaster) - Canoas, RS, Brazil > "Rumor has it that, when they shut down the IBM 7094 at MIT in 1973, > they found a low-priority process that had been submitted in 1967 and had > not yet been run. " -- From Silbershatz and Galvin, pp. 142-143. > >