Convert hexadecimal to binary - binary

I am converting the hexadecimal number 0XA85D10 to binary. However, I am told you can convert this number without using paper or a calculator.
Is this possible?
Thanks

The easiest way to convert from hex to binary is to split each digit in the hex into an 4 digit binary number. (i.e. D matches to 1110)
Using your example
A 8 5 D 1 0
1010 1000 0101 1110 0001 0000

Yes. It is possible. One hexadecimal digit is exactly four binary digits.
A = 1010
8 = 1000
...and so on.
If the digit is greater or equal to 8, then subtract 8 from the digit the first binary digit is 1, otherwise it is zero.
If the digit is now greater or equal to 4, then subtract 4 and the next digit out is 1, otherwise the next digit out is 0.
If the digit is now greater or equal to 2, then subtract 2 and the next digit out is 1, otherwise the next digit out is 0.
Whatever is left, 0 or 1, is the remaining digit.

Easily. Each digit in a hex number translates to 4 digits in a binary number, so you only need to know the binary numbers from 0 to f, or 0000 to 1111.
As an example:
0xc3e2
c = 12 decimal = 1100
3 = 0011
e = 14 decimal = 1110
2 = 0010
Then just string them together.
0xc3e2 = 1100001111100010 binary

You can convert Hexadecimal to Binary using the following (Hex -> Binary):
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111
Hope this helps!

public with sharing class HexToBin {
private String code;
public String result;
Map<String, String> hexToBinMap = new Map<String, String>{'0'=>'0000','1'=>'0001','2'=>'0010','3'=>'0011','4'=>'0100','5'=>'0101','6'=>'0110','7'=>'0111','8'=>'1000','9'=>'1001','A'=>'1010','B'=>'1011','C'=>'1100','D'=>'1101','E'=>'1110','F'=>'1111'};
public HexToBin(String code) {
this.code = code;
result = getResult(code);
}
private String hexToBin(String hex){
if(hex.length()==1){
return hexToBinMap.get(hex);
}
return '';
}
private String getResult (String code) {
String res ='';
for(Integer i=0; i<code.length(); i++){
res = res + hexToBin(code.substring(i,i+1));
}
return res;
}
}

Related

for a one-dimensional string array that converts every 2 bits from binary to decimal For example: str [] = {"10 1111 1010" } => 2 33 22

for a one-dimensional string array that converts every 2 bits from binary to decimal
For example: str [] = {"10 1111 1010" } => 2 33 22
str[]={"" 11 01 1010"}=>3 1 22enter image description here

Converting 0x0AF8 to binary

Can someone explain how this number is converted to binary ?
Number : 0x0AF8
It will be helpful for beginners to learn from
In hexadecimal format, there are 16 possible digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Each hexadecimal digit can be converted into 4 binary digits:
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111
Having said all that, you can convert 0x0AF8 to binary format as follows:
0 A F 8
0000 1010 1111 1000
Your Number is written in hex so if you want to convert it to binary you need to just set 4 bit for each character of hex number.
for example:
HEX: 0x0AF8
BINARY: 0000 1010 1111 1000
Actually you just change the representation not the content of number ...

How to read a 14 bit digit from a SPI interface

I am trying to read temperature data from a temperature conditioning chip and it says that the data is formatted as the first 14-bits of a 32 bit number. I understand the sign and the main part and the decimal part. But I cannot understand bit number 20. I thought it was a decimal place holder, but its value changes when they gave examples for the format. You can see that for +1600.00 --> 0110 0100 0000 00 and for +25.0 --> 0000 0001 1001 00. What does the 3rd bit from the left represent??
D20 is the 1's position (2^0).
D31 = Sign bit
D30 = 2^10 = 1024
D29 = 2^9 = 512
D28 = 2^8 = 256
D27 = 2^7 = 128
D26 = 2^6 = 64
D25 = 2^5 = 32
D24 = 2^4 = 16
D23 = 2^3 = 8
D22 = 2^2 = 4
D21 = 2^1 = 2
D20 = 2^0 = 1
D19 = 2^-1 = 0.5
D18 = 2^-2 = 0.25

Combining Bloom Filters

I am using bloom filters to check for duplicated data in a set. However, there is a need to combine the results of two sets of data into a single filter to check for duplication across the two sets. I devised a function in pseudo-Python to perform this task:
def combine(a : bloom_filter, b : bloom_filter):
assert a.length == b.length
assert a.hashes == b.hashes
c = new bloom_filter(length = a.length, hashes = b.hashes)
c.attempts = a.attempts + b.attempts
c.bits = a.bits | b.bits
# Determining the amount of items
a_and_b = count(a & b)
a_not_b = count(a & !b)
not_a_b = count(!a & b)
neither = count(!a & !b)
c.item_count = a_not_b / a.length * a.item_count
+ not_a_b / b.length * b.item_count
+ a_and_b / c.length * min(a.item_count, b.item_count)
return c
Does this even sound correct? I am having considerable internal debate as to whether is is even possible to do what I intend, since much of the information about the source data is lost (which is the point of a bloom filter).
You can derive a formula for estimating the amount of items a Bloom Filter:
c = log(z / N) / ((h * log(1 - 1 / N))
N: Number of bits in the bit vector
h: Number of hashes
z: Number of zero bits in the bit vector
This provides a fairly accurate estimate of the number of items in the Bloom Filter. You can come up with an estimate for contribution with simple subtraction.
It could be possible..... sort of..
lets say set A contains apples and oranges
lets say set B contains peas and carrots
construct a simple 16 bit bloom filter as an example and CRC32 as the hash
crc32(apples) = 0x70CCB02F
crc32(oranges) = 0x45CDF3B4
crc32(peas) = 0xB18D0C2B
crc32(carrots) = 0x676A9E28
Start w/ empty bloom filter (BF) (say 16 bits) for both sets (A, B)
BFA = BFB = 0000 0000 0000 0000
then, breaking the hash into some bit length, we'll use 4 here
we can add apples to the BF.
e.g.
Get Apples BF Index list by splitting up the hash:
0x70CCB02F = 0111 0000 1100 1100 1011 0000 0010 1111
7 0 C C B 0 2 F
----------------------------------------------------
Add Apples to BFA by setting BF bit indexes [ 7, 0, 12, 12, 11, 0, 2, 15]
(set the index bit of an empty BF to 1)
Apples = 1001 1000 1000 0101 (<- see indexes 0,2,7,11,12,15 are set)
BF = 0000 0000 0000 0000 (or operation adds that item to the BF)
================================
Updated BFA = 1001 1000 1000 0101
Add Oranges to BF same way:
0x45CDF3B4 = 0100 0101 1100 1101 1111 0011 1011 0100
4 5 12 13 15 3 11 4
----------------------------------------------------
Add oranges to BF by setting BF bit indexes [ 4,5,12,13,15,3,11,4]
Oranges = 1011 1000 0011 1000
BFA = 1001 1000 1000 0101 (or operation)
================================
Updated BFA = 1011 1000 1011 1101
So now apples and oranges are inserted into BF1
w/ Final Value of 1011 1000 1011 1101
Do the same for BFB
crc32(peas) = 0xB18D0C2B becomes =>
set [11,2,12,0,13,1,8] in BFB
0011 1001 0000 0011 = BF(peas)
crc32(carrots) = 0x676A9E28 becomes =>
set [8,2,14,9,10,6,7] in BFB
0100 0111 1100 0100 = BF(carrots)
so BFB =
0011 1001 0000 0011 BF(peas)
0100 0111 1100 0100 BF(carrots)
=================== ('add' them to BFB via locial or op)
0111 1111 1100 0111
you could now search B for A entries in a loop and vice verse:
Does B contain "oranges" =>
1011 1000 0011 1000 (Oranges BF representation)
0111 1111 1100 0111 (BFB)
===================== (and operation)
0011 1000 0000 0000
Because this result (0011 1000 0000 0000) doesn't match the
Original BF of Oranges, you can be certain that B doesn't contain any oranges
... ... (do for rest of items)
and following, B doesn't contain any of A items,
just as B doesn't contain any of the apples.
I don't think that's what you asked though, and looks like you could computer a difference
BF, which is more to your point. Seems like you could do a xor op and that would give you a 'single' array containing both differences:
0111 1111 1100 0111 (BFB)
1011 1000 1011 1101 (BFA)
========================
1100 0111 0111 1010 (BFA xor BFB) == (items in B not in A, and items in A not in B)
meaning with this single BF, you could detect the non-existance of an item 100% of the time,
just not the existance of the item 100%.
The way you would use it, is as follows (check if peas is 'missing from A):
1100 0111 0111 1010 (BFA xor BFB)
0011 1001 0000 0011 (Peas)
============================== (And operation)
0000 0001 0000 0010 (non-zero)
since (BFA xor BFB) && (Peas) != 0 you know one set does not contain 'peas'...
again, you'd be testing for item by item, maybe you could do aggregation but probably not a good idea...
Hope this helps!

x-y = x+¬y+1 problem

I am currently reading a book about "bit fiddling" and the following formula appears:
x-y = x+¬y+1
But this doesn't seem to work. Example:
x = 0100
y = 0010
x-y = 0010
¬y = 1101
¬y+1 = 1110
x+1110 = 10010
But 10010 != 0010...
Where did I make a mistake (if any)?
(The book is "Hacker's Delight" by Henry S. Warren.)
You only have a four bit system! That extra 1 on the left of your final result can't exist. It should be:
x = 0100
y = 0010
~y = 1101
~y + 1 = 1110
x + 1110 = 0010
The other bit overflows, and isn't part of your result. You may want to read up on two's complement arithmetic.
You are carrying the extra bit. In real computers if you overflow the word, the bit disappears. (actually it gets saved in a carry flag.) .
Assuming the numbers are constrained to 4 bits, then the fifth 1 would be truncated, leaving you with 0010.
It's all about overflow. You only have four bits, so it's not 10010, but 0010.
Just to add to the answers, in a 2's complement system:
~x + 1 = -x
Say x = 2. In 4 bits, that's 0010.
~x = 1101
~x + 1 = 1110
And 1110 is -2