Prev: constant in embedded matlab HDLCoder
Next: "Model-Based Design for DO-254"- ONE webinar session only- July 22nd @ 10:00am EDT
From: Kevin on 19 Jul 2010 15:51 Hi there. I've writen a function that converts bytes to bits, but now I'mhaving trouble writing a function that reverses the process. Or is there a built-in function that does this already? My coding looks like so: function bitData = byte2bit(data) lengthData = length(data); bitData = uint8(zeros(1,lengthData*8)); p = 1; for k = 1:1:lengthData for l = 8:-1:1 bit = bitget(data(k),l); bitData(p) = bitset(bitData(p),1,bit); p = p + 1; end end
From: Walter Roberson on 19 Jul 2010 19:43 Kevin wrote: > Hi there. I've writen a function that converts bytes to bits, but now > I'mhaving trouble writing a function that reverses the process. Or is > there a built-in function that does this already? My coding looks like so: > function bitData = byte2bit(data) > lengthData = length(data); > bitData = uint8(zeros(1,lengthData*8)); > p = 1; > for k = 1:1:lengthData > for l = 8:-1:1 > bit = bitget(data(k),l); > bitData(p) = bitset(bitData(p),1,bit); > p = p + 1; > end > end Have you considered using dec2bin() and subtracting the string '0' ? >> dec2bin(round(255*rand(1,5))) - '0' ans = 0 0 0 1 1 0 0 1 1 1 0 1 0 0 1 0 1 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1 1 0 0 1 0 Your question is underspecified as to which data types you need to work with. Your proposed code will not work with floating point data types.
From: Kevin on 19 Jul 2010 21:36 Walter Roberson <roberson(a)hushmail.com> wrote in message <i22o6h$b6v$1(a)canopus.cc.umanitoba.ca>... > Kevin wrote: > > Hi there. I've writen a function that converts bytes to bits, but now > > I'mhaving trouble writing a function that reverses the process. Or is > > there a built-in function that does this already? My coding looks like so: > > function bitData = byte2bit(data) > > lengthData = length(data); > > bitData = uint8(zeros(1,lengthData*8)); > > p = 1; > > for k = 1:1:lengthData > > for l = 8:-1:1 > > bit = bitget(data(k),l); > > bitData(p) = bitset(bitData(p),1,bit); > > p = p + 1; > > end > > end > > Have you considered using dec2bin() and subtracting the string '0' ? > > > >> dec2bin(round(255*rand(1,5))) - '0' > ans = > 0 0 0 1 1 0 0 1 > 1 1 0 1 0 0 1 0 > 1 0 1 1 0 0 0 1 > 0 1 0 1 0 0 0 1 > 1 1 1 1 0 0 1 0 Terribly sorry. That part of the code is irrelevant to my problem; should've mentioned that. The key thing is converting bits to bytes. Thanks for the tip. > Your question is underspecified as to which data types you need to work with. > Your proposed code will not work with floating point data types.
From: Walter Roberson on 19 Jul 2010 23:52
Kevin wrote: > Walter Roberson <roberson(a)hushmail.com> wrote in message > <i22o6h$b6v$1(a)canopus.cc.umanitoba.ca>... >> Kevin wrote: >> > Hi there. I've writen a function that converts bytes to bits, but >> now > I'mhaving trouble writing a function that reverses the process. >> Or is > there a built-in function that does this already? My coding >> looks like so: >> > function bitData = byte2bit(data) >> > lengthData = length(data); >> > bitData = uint8(zeros(1,lengthData*8)); >> > p = 1; >> > for k = 1:1:lengthData >> > for l = 8:-1:1 >> > bit = bitget(data(k),l); >> > bitData(p) = bitset(bitData(p),1,bit); >> > p = p + 1; >> > end >> > end >> >> Have you considered using dec2bin() and subtracting the string '0' ? >> >> >> >> dec2bin(round(255*rand(1,5))) - '0' >> ans = >> 0 0 0 1 1 0 0 1 >> 1 1 0 1 0 0 1 0 >> 1 0 1 1 0 0 0 1 >> 0 1 0 1 0 0 0 1 >> 1 1 1 1 0 0 1 0 > > Terribly sorry. That part of the code is irrelevant to my problem; > should've mentioned that. The key thing is converting bits to bytes. > Thanks for the tip. >> Your question is underspecified as to which data types you need to >> work with. Your proposed code will not work with floating point data >> types. And if you take a matrix of bit values, *add* '0' to the matrix, and then read the "see also" list in the description of dec2bin to find the routine that does the reverse of dec2bin ? |