Is there a function in R that performs the same operation as bitget in MatLab/Octave:
Asked
Active
Viewed 323 times
0
-
http://stackoverflow.com/questions/6614283/converting-decimal-to-binary-in-r – nico Jan 15 '15 at 18:55
-
Quick question, the results of the function in the questions above `paste(sapply(strsplit(paste(rev(intToBits(100))),""),`[[`,2),collapse="")` do not match the octave equivalent `bitget(100,1:30)` – mrkb80 Jan 15 '15 at 19:41
-
Ah, I see it. It is just reversed. – mrkb80 Jan 15 '15 at 19:53
1 Answers
2
From the bitget
help page
Return the status of bit(s) n of unsigned integers in A the
lowest significant bit is n = 1.
bitget (100, 8:-1:1)
⇒ 0 1 1 0 0 1 0 0
so if you want to get the bit values for an integer in R, you can do
intToBits(100)[8:1]
# [1] 00 01 01 00 00 01 00 00
That technically returns a raw vector, so if you want just a numeric vector, do
as.numeric(intToBits(100)[8:1])
# [1] 0 1 1 0 0 1 0 0

MrFlick
- 195,160
- 17
- 277
- 295