if an integer is stored on 4 octets which means its stored in 32 bits, so why the convertion from integer to bits have less than 32bits? - binary

*I mean that as we know an int is stored as 4 octets, that means if we want to convert for example 18 to binary, it must contain 32 bits not just 5 bits (18 = 10010 bits)
This works to with any other type, string, float, double etc.... we don't have the number of bits that normally we must have, the majority of type is less. So please can you explain to me, I'm sure I miss something.
*

Imagine you have a device with a long set of wheels, each showing a digit from 0 to 9. You can set each digit to one of those 10 digits, and nothing else. Your task is to agree with your friend a way to pass across a list of numbers.
Your first idea is to simply set the numbers directly, and leave any spare wheels at 0. I give you the numbers 5, 7, and 3, so you set your wheels like this:
5 7 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You pass it to your friend, and they read off "5", "7", "3".
But now I give you the numbers 18, 2006, and 0. So you set the wheels like this:
1 8 2 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You pass it to your friend, and they're confused. Is that "1", "8", "2006"? Or perhaps it's "18", "200", "6"? They don't know where one number ends and the next begins.
So, you have an idea: if all numbers were the same length, you'd know where they began and ended. Fortunately, in our number system, "1", "01", "001", and "0001" all mean the same thing, so you can agree that all numbers will be 4 digits long. 18, 2006, 0 now looks like this:
0 0 1 8 2 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0
You pass it over, and your friend reads off "0018, that's eighteen; 2006; 0000, that's zero". Success!
Our only problem is that we can't represent a number higher than 9999; so when we choose our fixed width, we need to decide on the highest number we want to be able to represent. More width, higher numbers; but more wasted space when we're working with smaller numbers.
When we talk about "32-bit integers" or "64-bit integers", we're talking about essentially the same idea, but instead of wheels that can be 0 to 9, we have some representation of bits that can be either 0 or 1.
So, to store the number 18, we work out the binary, 10010, and then somewhere in memory we write down a sequence of 32 bits:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
If we don't need very high numbers, and want to save space, we might make do with 16 bits, or even 8; if memory is cheap enough, we might use 64 bits to be able to have much higher numbers. But everyone reading and writing a particular set of numbers has to agree in advance what width to use.
Things get a bit more complicated if we want negative numbers, or fractions, but the aim is the same: by agreeing in advance how much space each number will take up, we know where one number begins and the next one ends.
Now let's think about strings.
Imagine we extend the earlier task to pass a word to your friend, using the same device. One way to do this is to devise a code where each letter is given a number: 1 for A, 2 for B, and so on. Then a string is just a sequence of numbers, using our earlier fixed length idea; we only need to go up to 26, so we'll choose a fixed width of 2 digits.
So given the word "HELLO", you would pass over 8, 5, 12, 12, 15 like this:
0 8 0 5 1 2 1 2 1 5 0 0 0 0 0 0 0 0 0 0
Your friend reads off the numbers, converts them to letters, and gets the word.
Let's make it more challenging: a word, followed by a number, for instance: "YEAR", 2006; or "AGE", 18.
2 5 0 5 0 1 1 8 2 0 0 6 0 0 0 0 0 0 0 0
0 1 0 7 0 5 0 0 1 8 0 0 0 0 0 0 0 0 0 0
Wait, is that "YEART" and then 600? Or maybe "YEARTF" and a zero? We're back to our original problem of knowing when things begin and end.
One solution is to use our fixed length trick again: let's say every word is five letters long, but if the letter value if "00", ignore it. Because we're not used to putting "zeroes" on the beginning of words, we decide to pad at the end instead of the beginning, but it doesn't really matter.
2 5 0 5 0 1 1 8 0 0 2 0 0 6 0 0 0 0 0 0
0 1 0 7 0 5 0 0 0 0 0 0 1 8 0 0 0 0 0 0
The problem is, words vary in length a lot more than numbers, so we'll often have to have quite a large width, and so waste a lot of space on smaller words.
But there's something interesting about our "00", it's essentially like a space in English writing. When we read, we know that as soon as we get to a space, that's the end of the word, so we can use the same trick with our 00.
2 5 0 5 0 1 1 8 0 0 2 0 0 6 0 0 0 0 0 0
0 1 0 7 0 5 0 0 0 0 1 8 0 0 0 0 0 0 0 0
That means we can write as much text as fits in our total number of spaces; for instance, to write "A", 99; "AA", 99; and "AAAAAAAA", 99:
0 1 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 9 9
This is one of the common ways strings are stored in computers - in practice, it's not words we want to separate, but whole strings, so the "terminator" (normally all zero bits) is separate from a space (which in ASCII-compatible 8-bit encodings is 00100000),
One problem is that you have to "reserve" that special terminating character - you can't represent a string that has that sequence of bits in it. To get around that, there's a different trick we can use: instead of signalling the end of the string, we can send a separate marker for the length of the string.
For instance, our "YEAR" and "AGE" example becomes:
0 4 2 5 0 5 0 1 1 8 2 0 0 6 0 0 0 0 0 0
0 3 0 1 0 7 0 5 0 0 1 8 0 0 0 0 0 0 0 0
And our As and 9s:
0 1 0 1 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 0 1 0 1 9 9 0 0 0 0 0 0 0 0 0 0 0 0
0 8 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 9 9
The maximum length of a string is now determined by how wide we made the number representing the length - in this case, we have a two-digit length, so can represent string from 0 to 99 letters long.

Related

Django - How to properly stream a matrix in a RESTful API?

After computing some values from on the server side. The result is a matrix (i.e a pandas.DataFrame) with the following format:
0 1 2 3 4 5 6
0 1 0 0 0 2 0 6
100 0 0 0 0 0 0 0
200 0 0 0 0 0 0 0
300 0 0 0 0 0 0 0
Where first row/column represents row/column indexes, and (just in this case) some of the values are zero.
What would be a popper JSON format to stream this matrix? Is there any standard way to do it?
I suggest
{
x_axis: [0,1,2,3],
y_axis: [100,200,300],
rows: [
[1,2,3,4],
[1,2,3,4],
[1,2,3,4]
]
}

Dynamic JSON editing in r

I am using the jsonlite package to work with a complex json file in r. To give a sense of its complexity, here is a look into two different data frames it contains
> output$dataobjects[1]$dataobject$scoredresults$x
xid rawscore tscore percentile standarderror omit
1 LAC.1 0 0 0 0 0
2 LAC.5 0 0 0 0 0
3 LAC.9 0 0 0 0 0
4 LAC.14 0 0 0 0 0
> output$dataobjects[1]$dataobject$scoredresults$y
yid rawscore tscore percentile standarderror omit
1 c1.1.1 0 0 0 0 0
2 c1.1.3 0 0 0 0 0
3 c1.1.5 0 0 0 0 0
4 c1.1.6 0 0 0 0 0
I would like to dynamically edit this json based on an external dataframe. Say the dataframe looks like this
id <- c("LAC.1", "LAC.5", "c1.1.1", "c1.1.5", "LAC.14")
rawscore <- c(15, 10, 12, 14, 15)
type <- c("x", "x", "y", "y", "x")
df <- data.frame(id, rawscore, type)
I want to use a for-loop to iterate over the rows in this dataframe, updating the rawscore column in the json as I go. I can't figure out how to move from x to y within the json while staying within the for-loop. Please help!

Anyway to prove the boolean algebra theories 9 and 10?

I have a problem grasping the fact that A + AB = A and A(A + B) = A
Can anyone tell me how?
To understand this, look at the truth tables (1=true, 0=false):
"A" "B" "AB" "A+B" "A + AB" "A(A+B)"
0 0 0 0 0 0
0 1 0 1 0 0
1 0 0 1 1 1
1 1 1 1 1 1

Boolean Algebra simplification

is it possible to simply this Boolean function
(!A*!B*!C) + (!A*!B*C*!D) + (A*!B*!C*D) + (A*!B*C*!D) + (A*B*!C*!D)
I came up with this:
(!B*(!A*(!C+!D))+A*(C XOR D)) + (A*B*!C*!D)
Messy to look at, but there are fewer terms.
Look at the truth table:
A B C D X
0 0 0 0 1
0 0 0 1 1
0 0 1 0 1
0 0 1 1 0
0 1 0 0 0
0 1 0 1 0
0 1 1 0 0
0 1 1 1 0
1 0 0 0 0
1 0 0 1 1
1 0 1 0 1
1 0 1 1 0
1 1 0 0 1
1 1 0 1 0
1 1 1 0 0
1 1 1 1 0
It looks like you can take the three parts of the table where X = 1 and simplify this to the sum of three terms:
!A*!B*!(C*D) + A*!B*(C^D) + A*B*!C*!D
Note that I've use XOR (^) in the second term. If you can't use XOR then you'll need to expand the second term a little.
You can reduce the number of terms further by factoring out either !B or A for two of the terms, e.g.
!B*(!A*!(C*D) + A*(C^D)) + A*B*!C*!D
or:
!A*!B*!(C*D) + A*(!B*(C^D) + B*!C*!D)

octave matrix: replace 0's with 1's and replace 1's with 0's

I have a matrix of 0's and 1's, say:
0 1 0 0
0 0 1 0
1 0 0 0
I want to generate another matrix that replaces 0's with 1's and 1's with 0's:
1 0 1 1
1 1 0 1
0 1 1 1
Anyone know how to do this in Octave?
b = 1 - a;