Encode data into 1 byte - binary

I have to encode data to 1 byte. I have the following data as of now.
size - 500 ml and 1 litre
Frequency - 0 to 12
% - 0-100
So i decided to break the data into the following -
0 0 0 0 0 0 0 0
1st bit - Size - 0 for 500ml and 1 for 1 litre
2-5 bits - Frequency - 0 to 12 (0000 for 0 and 1100 for 12)
I am not sure how to get the % in this setting. Am i looking into solving this in a wrong way? Is there any other way to do it. Any direction is highly appreciated.

You are left with 3 bits. you need to store a value between 0-100, which atleast needs 7 bits. (2^7 = 128). However, if you only need 8 different percentage values, you can get away with using 3 bits

Related

Algorithm for selecting tiles outwards center point in a 512*512 map

I've got a specific problem. My data (map) in mysql is as follows
id table_row table_col tile_type
1 1 1 0
2 2 1 0
3 3 1 0
... ... ... 0
512 512 1 0
513 1 2 0
514 2 2 0
515 3 2 0
... ... ... 0
... 512 2 0
... 1 3 0
... 2 3 0
... 3 3 0
... ... ... 0
... 512 3 0
... 1 4 0
Map is 512*512. I need to come up with an algorithm that selects tiles from the centre(or near centre 256*256) point. So it should look something like
256*256 first - once selected we can update tile_type to 1
255*256 second - update tile_type to 1
256*255 third - update tile_type to 1
257*256 fourth - update tile_type to 1
256*257 fifth - update tile_type to 1
etc. or similar, but it has to start filling in tiles from centre outwards in all directions (can be random). Any ideas appreciated
Your question lacks a few details, but I am assuming you are asking a means of generating an id that is close to the center of your 512x512 grid.
It appears your grid is enumerated in a particular manner: each column is enumerated in increasing order of table_row values, and the enumeration of columns is done in increasing order of table_col values.
Consequently, we can already know the id of the cell for which the table_row and table_col values are 256: it is 255 x 512 + 256. That is correct, because there are 255 full columns that were enumerated before enumeration started for table_col value 256, and each of those columns had 512 rows in them. Finally, within this column, we are interested in row #256.
A more generalized version of this would look like below.
((num_cols + 1) / 2 - 1) * num_rows + (num_rows + 1) / 2
You don't need to care all that much about the +1s and -1s: they are just a numerical hack to handle odd num_rows and num_cols values.
Anyways, to introduce a proximity measure, you can just use two random variables. A random variable P can represent the distance to the center in terms of colums. (i.e. how far the table_col of the point with the generated id will be from the table_col value of the center of the grid) Another random variable Q can represent the distance to the center in terms of rows.
((num_cols + 1) / 2 - 1 + P) * num_rows + ((num_rows + 1) / 2 + Q)
Then you can just generate values for P and Q based on your needs, and get the id of a cell that is P colums and Q rows away from the center of the grid.
Try Below query.
SELECT (MAX(t.`row`+1)/2), (MAX(t.`column`+1)/2) INTO #max_row, #max_col
FROM tiles t;
SELECT t.`row`, t.`column`, ceil(IF(ABS(#max_row - t.`row`) < ABS(#max_col - t.`column`), ABS(#max_col - t.`column`), ABS(#max_row - t.`row`))) as tbl_order
FROM tiles t
ORDER BY 3

convert int to binary in smalltalk visualworks

I have a weird issue that I cant seem to resolve so hope that converting this to some other form will help:
|coder response|
(coder isBitSet: 1)
ifFalse:[self flagSuccess]
ifTrue:[self flagFailure].
now the issue is coder is a value from 0 to F, when I get a 5 I want it to be treated as 0101, so that it is FALSE from isBitSet:. BUT isBitSet: treats it as 101, so it's always true... so basically isBitSet: isn't working for any binary number thats 4 bits long UNLESS the number is zero...
how can I get my five so that I can check the 4th bit in the number for a 1 or 0?
Try using the bitAt: method. This method extracts a bit from an integer and tells you whether it's a 1 or a 0. It will extract any bit you want and treat bits higher than the size of the integer as 0's.
5 bitAt: 1 ==> 1
5 bitAt: 2 ==> 0
5 bitAt: 3 ==> 1
5 bitAt: 4 ==> 0
5 bitAt: 5 ==> 0
Does that help?
You might not be interpreting the bit numbering correctly. The reason why 5 isBitSet: 1 evaluates to true is that 1 refers to the lowest bit. Regardless of whether 5 is represented as 101, 0101 or even 00101 etc., the lowest bit is always 1 and 5 isBitSet: 1 answers with true.

Why Do You Need To Minus 1 When Determining The Maximum Number N Bits Can Represent?

So in binary to find the largest number you can represent given N amount of bits, you would use:
2^N - 1
But why the -1. To try understand it i created a 3 Bit systems and tried some examples:
2^1 = (2) - 1
0 0 1 --> 1
2^2 = (4) - 1
0 1 0 --> 2
0 1 1 --> 3
2^3 = (8) - 1
1 0 0 --> 4
1 0 1 --> 5
1 1 0 --> 6
1 1 1 --> 7
So it all works out as planned, but why the -1. This probably sounds like a stupid question but as you can see above i have done a fair amount of research.
Because you can represent 0 which always takes up one spot in all the permutations.
The research shown should reveal the answer already, but you have forgotten about the zero.
Three bits are able to represent 2^3 different values. The smallest value is zero, so the largest must be 2^3-1.
Note that if you use a different system (such as signed binary), the smallest and largest value may change, but the count of values does not.

Does storing ASCII files as a BLOB field in MySQL or Oracle offer a reduction in storage requirements?

I have several thousand small ASCII files containing 3D cartesian coordinates for atoms in molecules (among other information) that I need to store somewhere.
A simple calculation told me that we will require several terrabytes of space, which may be reduced to several gigabytes at most, but is not manageable under current infrastructural constraints. Somebody told me some people have stored similar numbers of files (of the same format, but sometimes bzipped) in MySQL and Oracle as a BLOB field. My question is, does storing such files as BLOB offer some form of reduction in storage requirements? If yes, how much of a reduction can I expect?
This is example text from an ASCII file that needs to be stored:
#<TRIPOS>MOLECULE
****
5 4 1 1 0
SMALL
GAST_HUCK
#<TRIPOS>ATOM
1 C1 -9.7504 2.6683 0.0002 C.3 1 <1> -0.0776
2 H1 -8.6504 2.6685 0.0010 H 1 <1> 0.0194
3 H2 -10.1163 2.1494 -0.8981 H 1 <1> 0.0194
4 H3 -10.1173 3.7053 -0.0004 H 1 <1> 0.0194
5 H4 -10.1176 2.1500 0.8982 H 1 <1> 0.0194
#<TRIPOS>BOND
1 1 2 1
2 1 3 1
3 1 4 1
4 1 5 1
#<TRIPOS>SUBSTRUCTURE
1 **** 1 TEMP 0 **** **** 0 ROOT
#<TRIPOS>NORMAL
#<TRIPOS>FF_PBC
FORCE_FIELD_SETUP_FEATURE Force Field Setup information
v1.0 0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 NONE 0 0 0 0 1 0 0 0 0 0 0 0 0
Storing data in a BLOB column offers no form of reduction in storage requirements. The storage requirements for BLOB types are simple:
TINYBLOB L + 1 bytes, where L < 28
BLOB L + 2 bytes, where L < 216
MEDIUMBLOB L + 3 bytes, where L < 224
LONGBLOB L + 4 bytes, where L < 232
L represensts the length of the string data in bytes.
See Storage Requrements for further details.
If there is no need to search the contents of the molecule files in your database, you can reduce the storage requirements by compressing the data prior to inserting it or using the MySQL COMPRESS() function on insert.
I think that addressed your main question, and based on those figures and how many files you plan to store based on an average size, you can calculate how much storage space will be consumed by the BLOB type columns.

Simple Line Graph in SSRS

I'm having issues using the line chart control in SSRS. If I was in excel this would be easy but for some reason I can't wrap my head around how to do it in SSRS
I'm returning 5 rows of data from a database and need to chart 3 of the rows.
Name J F M A M J J A S
1 Requested 13 19 4 20 2 0 0 0 0
2 Completed 1 0 0 4 1 0 0 0 0
3 % Completed .7 0 0 0.2 0.5 0 0 0 0
4 Monthly Ba 12 19 4 16 1 0 0 0 0
5 YTD Backlog 12 31 35 51 52 52 52 52 52
The rows consist of the name and the value for that month.
I'm trying to display a chart for rows 1, 2, and 5 that would look like a normal line graph with each of the Months being on the horizontal column and the value being the data point.
However, when I start adding things to my chart I'm getting 36 different series, one for each Month for each series and nothing seems to be working right. It's also splitting into Group 1 and 2 at the bottom which makes no sense to me. I feel like I'm missing something simple, most likely a grouping of some kind.
EDIT:
I ended up taking Nathan's suggestion and added an unpivot on the table to rotate the data into an acceptable format for SSRS
Thanks
Can you alter the query that is returning the results for the dataset?
If so try changing it so that the dataset looks more like this:
monthnumber month Requested Completed YTD Backlog
1 J 13 1 12
2 F 19 0 31
3 M 4 0 35
4 A 20 4 51
etc.
You should be able to add "Requested", "Completed" and "YTD Backlog" as Values and "month" as a Category to produce the chart you want.
I added a "monthnumber" column, as you will probably want something like this so that you can set the category (month) sorting expression to this, otherwise they will appear in alphabetical order.