Converting base 2 to base 16 [duplicate] - math

This question already has answers here:
Converting binary to hexadecimal?
(5 answers)
Closed 9 years ago.
I have seen many people get confused about how to convert base 2 to base 16 directly. In this tutorial I will explain how to convert a Binary number to a Hexadecimal number in 5 easy steps.

1) When you have a number in base 2, all digits must be either 0 or 1. If you have a digit(s) that isn't 0 or 1, your number is not in base 2 (Binary) and this tutorial won't be of use for you.
2) Make sure the length of you number is divisible by 4 (4,8,12,16 etc...). In this tutorial I will use 10001111011 in base 2 as the base number. Notice there there are only 11 digits. to make it divisible by 4 we will add a 0 to the left side of the number and check if the length is divisible by 4, keep on adding 0's until it is divisible.
3) Part your base 2 number into groups of four. In our case, 010001111011 will be 0100 0111 1011.
4) Now use the following table to convert each group of four digits to its matching value in base 16:
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = A
1011 = B
1100 = C
1101 = D
1110 = E
1111 = F
5) As a reminder, out number was 0100 0111 1011. Then 0100=4, 0111=7, 1011=B. Therefore. 010001111011 in base 2 is 47B in base 16(hexadecimal).

Related

seq_along() - truncating a replication in r

I would like to generate the month number to go along with a list of values. The problem is that the list is not a full 2 replications of 12 months. It is 12 from the first year and 10 from the second year.
tibble(value=rnorm(22))
Some things I have tried are rep(1:12,2), thinking that the sequence would stop
when it hit the end of the length of the dataframe. I also tried seq_along(along.with=value,1:12) with the same line of thinking.
You want the length.out argument to rep():
rep(1:12, length.out = 22)
which gives
> rep(1:12, length.out = 22)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10
We get this because, from ?rep:
‘length.out’ may be given in place of ‘times’, in which case ‘x’
is repeated as many times as is necessary to create a vector of
this length. If both are given, ‘length.out’ takes priority and
‘times’ is ignored.
I would roll out 22 months and then use a modulo operator to get months in subsequent year(s)
library(dplyr)
tibble(value=rnorm(22)) %>%
mutate(month=1:22,
month=ifelse(month%%12==0, 12, month%%12)

Adding leading zeroes to dataframe column using sprintf in R [duplicate]

This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 6 years ago.
I have a dataframe and want the number variable to be four digits long, in order to do this I need to add between 1-3 leading zeroes, the method I chose to do this is the sprintf function, as it is immaterial that the number is converted to character class.
Unfortunately the results are not coming out in the order I want
The test data frame is made as follows and the leading 0 column added on as a third column to allow easy comparison. As can be seen by running the code the order that the leading zero numbers are pasted in does not correspond to the original number order
test <- as.data.frame(cbind(letters,seq(from=1, to=26)))
test[,3]<-sprintf("%04d", test[,2])
by rearranging the data frame order alphabetically by classing the original number column as characters, the sprintf number are now in ascending order although the number series is not.
test.two <- as.data.frame(cbind(letters,seq(from=1, to=26)))
test.two <- test.two[i <-order(as.character(test.two[,2])),]
test.two[,3]<-sprintf("%04d", test.two[,2])
I can create the desired data set by Frankensteining it togther.
test.three <- as.data.frame(cbind(letters,seq(from=1, to=26)))
test.three[,3]<-test.two[,3]
However I would like to know what I am doing wrong and what method would give me the result I expected to get from what I thought was a simple operation!
This is due to the the second column being a factor.
test <- as.data.frame(cbind(letters,seq(from=1, to=26)))
sapply(test, class)
## letters V2
## "factor" "factor"
test[,3]<-sprintf("%04d", test[,2])
as.numeric(test$V2)
## [1] 1 12 20 21 22 23 24 25 26 2 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18
## [26] 19
test$V2 <- as.integer(as.character(test$V2))
test[,4]<-sprintf("%04d", test[,2])
## letters V2 V3 V4
## 1 a 1 0001 0001
## 2 b 2 0012 0002
## 3 c 3 0020 0003
## 4 d 4 0021 0004
## 5 e 5 0022 0005
## 6 f 6 0023 0006

Combination with a minimum number of elements in a fixed length subset

I have been searching for long but unable to find a solution for this.
My question is "Suppose you have n street lights(cannot be moved) and if you get any m from them then it should have atleast k working.Now in how many ways can this be done"
This seems to be a combination problem, but the problem here is "m" must be sequential.
Eg:
1 2 3 4 5 6 7 (Street lamps)
Let m=3
Then the valid sets are,
1 2 32 3 43 4 54 5 65 6 7Whereas,1 2 4 and so are invalid selections.
So every set must have atleast 2 working lights. I have figured how to find the minimum lamps required to satisfy the condition but how can I find the number of ways in it can be done ?
There should certainly some formula to do this but I am unable to find it.. :(
Should always be (n-m)+1.
E.g., 10 lights (n = 10), 5 in set (m = 5):
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
Gives (10-5)+1 = 6 sets.
The answer should always be m choose k for all values of n where n > m > k. I'll try to explain why;
Given, for example, the values m = 10, n = 4, k = 2, you can start by generating all possible permutations of 1s and 0s for sets of 4 lights, with exactly 2 lights on;
1100
0110
0011
1001
0101
1010
As you can see, there are 6 permutations, because 4 choose 2 = 6. You can choose any of these 6 permutations to be the first 4 lights. You then continue the sequence until you get n (in this case 10) lights, ensuring that you only ever add a zero if you must in order to keep the condition true of having 2 lights on for every 4. What you will find is that the sequence simply repeats; for example:
1100 -> next can be 1, so 11001
Next can still be 1 and meet the condition, so 110011.
The next must now be a zero, giving 1100110, and then again -> 11001100. This simply continues until the length is n : 1100110011. Given that the starting four can only be one of the above set, you will only get 6 different permutations.
Now, since the sequence will repeat exactly the same for any value of n, it means that the answer will always be m choose k.
For your example in your comment of 6,3,2, I can only find the following permutations:
011011
110110
101101
Which works, because 3 choose 2 = 3. If you can find more, then I guess I'm wrong and I've probably misunderstood again :D but from my understanding of this problem, I'm certain that the answer will always be m choose k.

tricky binary subtraction

So I was practicing my binary subtraction. It's been a long while since my first exam and I decided to create my own tricky binary subtraction and I came up with this one:
1100
-1101
Of course the "borrowing trick" does not work for this problem at least I could not get it to work. Is my only choice to flip the bits of the second binary number(the bottom one) and then add a one basically doing 2's complement so 1101 becomes 0011. Then add the primary binary number(1100) with the 2's complement representation(0011) which means it would look like this:
1100 (-4) assume 2's complement
+ 0011 (3) assume 2's complement
sum:1111 (-1) assume 2's complement
I just need confirmation on this problem since its been a long time since I did binary subtraction.
1100
-1101
0 - 1 = 1 (borrow 1)
1100
-1101
1
=====
1
0 - 0 - 1 = 1 (borrow 1)
1100
-1101
11
=====
11
1 - 1 - 1 = 1 (borrow 1)
1100
-1101
111
=====
111
1 - 1 - 1 = 1 (borrow 1)
1100
-1101
1111
=====
1111
The result is 1111 with 1 borrowed. In terms of unsigned arithmetic, this means that either the result underflowed or you need to borrow from the next significant digit. (In terms of signed arithmetic there is no overflow as you have also borrowed the second bit and the calculation corresponds to -4 - -3 = -1.)

"Calculations" of 6-digit octals in 8's complement, in layman's terms?

How does one find the postive/negative range of a 6-digit octal in 8's complement? From what I understand, it's simply half - 1:
000001 - 377777 positives, 400000 - 77777 negatives
Would 377777 be the highest positive, and 777777 the highest negative number?
If that is correct, it seems to me that 377777 and 777777 are complements of each other. However, from what I've read, to find the complement, subtract the number from the base. Doing that, 377777 would equal:
7 - 3 = 4
7 - 7 = 0
7 - 7 = 0
7 - 7 = 0
7 - 7 = 0
7 - 7 = 0
= 400000
This shows 400000 is the complement of 37777 (which appears inverse to me).

Resources