LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

convert number to 4 bit binary array

hi guys

this place is full of solutions and can count on for any problem. 

I wanted to convert a number in to Boolean array but since lowest representation is a byte I am getting an array of 8 bits when I need array of 4 bits.

thanks and regards 

Baig

0 Kudos
Message 1 of 19
(6,123 Views)

Well, you could use a nibble (Yep, that is the term for 1/2 of a byte).  just split your 8 bit array into two four bit arrays

Capture1.PNG

 

Of course, that EXPLODES the memory needed from 1 byte to a lot (1 for the U8, 8 for the array of Boolean, four for the I32 index and a couple more for the array sizes)


"Should be" isn't "Is" -Jay
0 Kudos
Message 2 of 19
(6,113 Views)

HEY Thanks a lot for your response and time. I really appreciate it.

I also needed to be caution about memory use and time of execution. what I did is used array subset to get a nibble or half byte since I needed only first 4 bytes and there is the same problem of memory consumption as you said. 

If these are the only ways of doing it then I would like to know which would be preferred for real time use and min consumption of time and memory. 

Thanks and Regards

baig

0 Kudos
Message 3 of 19
(6,072 Views)

@mabaig wrote:

HEY Thanks a lot for your response and time. I really appreciate it.

I also needed to be caution about memory use and time of execution. what I did is used array subset to get a nibble or half byte since I needed only first 4 bytes and there is the same problem of memory consumption as you said. 

If these are the only ways of doing it then I would like to know which would be preferred for real time use and min consumption of time and memory. 

Thanks and Regards

baig


Use the byte!.... Seriously, it requires only 2x the physical memory absolutely needed for a sextet .... ya got a 64bit bus! so the extra time to move data over that bus is nonsense! you need to move "Data" it fits into one bus at one time. 4, 8, 16, 32, 64 bits doesn't really matter except for the zeroes


"Should be" isn't "Is" -Jay
Message 4 of 19
(6,069 Views)

I am just curious: If we use fixed point number with length of 4 bits like this:

Fixed.JPG

The Number To Boolean Array function mentions that boolean array can have 8, 16, 32, 64 elements if we use integer, but can have flexible size if we use fixed point, in this case 4.

 

So if each boolean is a byte, we have 8 bytes from fixed point and 4 from boolean array which is 12 bytes total. If we use integers, we use (as you said) 1 for u8, 8 for bools, 4 for the index and then some, so it would be 13 bytes +.

 

Or do fixed points represented as decimals incur additional cost?

Atis

0 Kudos
Message 5 of 19
(6,056 Views)

@JÞB wrote:

Of course, that EXPLODES the memory needed from 1 byte to a lot (1 for the U8, 8 for the array of Boolean, four for the I32 index and a couple more for the array sizes)


Think you missed the <s> ?

 

But really, a boolean requires a byte? 😮


GCentral
0 Kudos
Message 6 of 19
(6,050 Views)

@cbutcher wrote:

@JÞB wrote:

Of course, that EXPLODES the memory needed from 1 byte to a lot (1 for the U8, 8 for the array of Boolean, four for the I32 index and a couple more for the array sizes)


Think you missed the <s> ?

 

But really, a boolean requires a byte? 😮


Yep,  unless you grab a 4.x and place it in a type cast. (Don't do that)


"Should be" isn't "Is" -Jay
0 Kudos
Message 7 of 19
(6,037 Views)

Ok, so moving back a bunch of steps...

What do you need this boolean array for?

It seems like the sensible options are:

  • Suck it up and take the minor memory penalty (best if you aren't doing this a billion times in parallel) - benefit is easier readability and simpler/more maintainable code. Probably worth more than ~10 bytes
  • Use Rotate Right with Carry in a loop and just use one element at a time? (maybe this reduces the memory cost, but you never get an array - depends why you need this)
  • Use 4 copies of Rotate R w/ Carry and get your 4 booleans (probably reduces compared to array of 8, prevents the use of multiple iterations which might be problematic if you need this in one cycle and are not in a SCTL (although, why would you have those two conditions?)). Not sure if this is a good plan or not...

Example_VI.png


GCentral
0 Kudos
Message 8 of 19
(6,030 Views)

@JÞB wrote:

@cbutcher wrote:

@JÞB wrote:

Of course, that EXPLODES the memory needed from 1 byte to a lot (1 for the U8, 8 for the array of Boolean, four for the I32 index and a couple more for the array sizes)


Think you missed the <s> ?

 

But really, a boolean requires a byte? 😮


Yep,  unless you grab a 4.x and place it in a type cast. (Don't do that)


I want to add here that LabVIEW is far from the only language that does this.  Almost everything in computer registers are accessed at the byte level.  So it is just faster to let a Boolean be represented by a full byte.  0 is a FALSE.  Anything else is a 1.

 

But are we seriously worried about these few extra bytes of memory?



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 9 of 19
(6,020 Views)

Hey all... Thanks for the response

Well I've checked the execution here and I guess it doesn't make any big difference and hence can use either of the way. Of course, it's better to use byte but I am forming 32 bit Boolean array from different numbers. That's why only needed 4 bits from some numbers. 

Thanks and regards

Baig

0 Kudos
Message 10 of 19
(5,982 Views)