"A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward."
Should we consider 2015 in binary format (111 1101 1111) a palindrome? This is feasible if we can neglect the leftmost 0, but can we do that? I mean in binary each four bits correspond to a decimal digit so we must have a leftmost zero which makes 2015 = 2000+0+10+5 = 0111 1101 0000+0000+1010+0101 = 0111 1101 1111 so can it be considered a palindrome? I know this can seem a dump theoretical question but I want to know the answer. Thanks in advance
11111011111 binary is 2015 decimal, and adding 0's to the left doesn't change its value. Note that you also mentioned: "in binary each four bits corresponds to a decimal digit". That's not the case. Here's an example of how binary corresponds to decimal:
So yes, you can neglect the leftmost 0. And 2015 is a palindrome.
Related
I've found such numbers time and time again in verilog and digital logic exercises, but I don't know how I'm supposed to know what number this is or how to write it as 12'b in verilog code.
Can someone explain them to me?
Presumably it's a 12-bit value in hexadecimal notation. Since one hexadecimal digit encodes 4 bits, there are 3 hexadecimal digits:
hexadecimal 0 corresponds to 4 bits 0000
hexadecimal 6 corresponds to 4 bits 0110
hexadecimal f corresponds to 4 bits 1111
So the same value in binary notation is 000001101111.
In Verilog you can write
12'h06f
or
12'b000001101111
Where 12' means "12 bits", h means "hexadecimal" and b means "binary".
The number in the post is known as a literal constant or a number literal or just a literal.
There is a short tutorial document with examples that is a good reference on the subject.
The document can be found here:
https://web.engr.oregonstate.edu/~traylor/ece474/beamer_lectures/verilog_number_literals.pdf
The first page looks like this:
The answer is not so simple. I know that generally 1 hex digit is mapped on 4 bits, but this is not even true. What appen if the MSB (the most left) is a 0?
Online I noticed that this string is converted sometimes to 476 digits, sometimes to 478 digits and sometimes to 480 digits? How do you convert it? And why? It's 120 digit long. Thank you.
02012B1530A6E3958A98530031902003876940000000000CDF9844173BE512AFFFFFFE11DBBA1F00079387800E13012E11FC017FFFFFFFFE39C10F40
It is true, that one hex digit corresponds to 4 bits.
So you just covert
0 => 0000
...
f => 1111
When you do this with the given number it starts with 0000 0010. Like with decimal numbers, leading zeros can be removed or added as you like, so all answers with more or less leading zeros can be correct.
Hex and binary are just other number systems like decimal.
In case of integers, you typically want to remove all leading zeros, lime with decimal numbers.
but when it is a byte or bit array that represents data, you would keep them, because 0 actually represents the value 0.
Ihr calculator that gives you 480 as result probably interprets it as a byte or bit array and therefore keeps the leading zeros.
476 seems to remove leading hex zeros and then keeps eventual the up to 3 leading binary zeros. No idea how you would end up with 478 digits.
However the common outputs should have 480 or 474 digits, depending if all zeros are kept or none. Everything else does not really make sense, eventhoug it is not technically wrong.
this is a school assignment. I've been given homework and one of the problems is to figure out the value after adding two hexadecimal values.
0x80000000 and 0xD0000000. I understand that D = 13, but I don't understand how the answer is 15, because 8 + 13 = 23? Could someone explain what I am doing wrong, and what I should do instead?
Thanks!
It's easy if you think that every digit represents a quadruple, for example
0xDEADBEEF = 13*16⁷+14*16⁶+10*16⁵+13*16⁴+11*16³+14*16²+14*16¹+15*16⁰.
The above hexadecimal value needs an algorithm to translate to a format the the ALU can add, for instance binary numbers.
D is 13 in decimal because D is digit number 13 if A replaces 10 and so on (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). The position of D is 7, so the number is 13*16⁷.
We notice that it is easier to LSB if we do this, and recognize that F is 15 in hexadecimal notation and therefore the number to the left will be 15*16⁰=15.
0xFF therefore means 15*16¹+15*16⁰=15*16+15=255 (you can check this with a calculator).
Now the algorithm is hopefully clear.
3735928559 is the decimal value of DEADBEEF because ==13*16^7+14*16^6+10*16^5+13*16^4+11*16^3+14*16^2+14*16^1+15*16^0=3735928559.
Some times I convert the hexadecimal into binary base 2 this because I feel more confident to do arithmetic with binary base 2than hexadecimal.
In order to do so you need to arrange every hexadecimal into group of 4 bits binary number.
hex 0x8 + 0xD
Convert to binary
binary 1000 + 1101 = 10101 = 21
group it again as 4 bits
0001 0101 = 0x15
I ignored if it's signed number and didn't used two's complement.
I am changing an 8-digit hexadecimal number to a real number Y.
The number is 0xAC0396ED
My question is: Shall I take into consideration the 0x? What is the significance of the 0x?
I researched it and based on wikipedia, I got this:
"use the prefix 0x for numeric constants represented in hex"
What I plan on doing is take the part AC0396ED and change it to binary, then from binary manipulate the 32 bit number byte dividing the number intro 3 parts: sign, exponent, and fraction.
My last question is why do we need hexacdecimal, decimal, octal? Why don't we just stick to binary in all our arithmetic and operations?
Thank you.
My question is: Shall I take into consideration the 0x? What is the significance of the 0x?
No - you should not take that into consideration - its only a notation for hexadecimal base system
What I plan on doing is take the part AC0396ED and change it to binary, then from binary manipulate the 32 bit number byte dividing the number intro 3 parts: sign, exponent, and fraction.
Here is how you calculate it:
AC0396ED =>
10 12 0 3 9 6 14 13 in decimal. Then to binary
1010 1100 0000 0011 1001 0110 1110 1101
here you have all bits i.e 32 bits
1 | 01011000 | 00000111001011011101101
so the first bit is 1 => the number is negative
the second is the exponent
the third is the mantissa
My last question is why do we need hexacdecimal, decimal, octal? Why don't we just stick to binary in all our arithmetic and operations?
Its much more easy to stick to hexadecimal system - compare the 32 bits to the AC0396ED
Just wondering on how I would go about converting binary to hexadecimal??
Would I first have to convert the binary to decimal and then to hexadecimal??
For example, 101101001.101110101010011
How would I go about converting a complex binary such as the above to hexadecimal?
Thanks in advance
Each 4 bits of a binary number represents a hexadecimal digit. So the best way to convert from binary to hexadecimal is to pad the binary number with leading zeroes so that the number of bits is divisible by four.
Then you process four bits at a time and convert them to a single hexadecimal digit:
0000 -> 0
0001 -> 1
0010 -> 2
....
1110 -> E
1111 -> F
No, you don't convert to decimal and then to hexadecimal, you convert to a numeric value, and then to hexadecimal.
(Decimal is also a textual representation of a number, just like binary and hexadecimal. Although decimal representation is used by default, a number doesn't have a textual representation in itself.)
As a hexadecimal digit corresponds to four binary digits you don't have to convert the entire string to a number, you can do it four binary digits at a time.
First fill up the binary number so that it has full groups of four digits:
000101101001.1011101010100110
Then you can convert each group to a number, and then to hexadecimal:
0001 0110 1001.1011 1010 1010 0110
169.BAA6
Alternatively, you can split the number into the two parts before and after the period and convert those from binary. The part before the period can be converted stright off, but the part after has to be padded to be correct.
Example in C#:
string binary = "101101001.101110101010011";
string[] parts = binary.Split('.');
while (parts[1].Length % 4 != 0) {
parts[1] += '0';
}
string result =
Convert.ToInt32(parts[0], 2).ToString("X") +
"." +
Convert.ToInt32(parts[1], 2).ToString("X");
You could simply have a small hash table, or other mapping converting each quadruplet of binary digits (as a string, assuming that's your input) into the corresponding hex digit (0 to 9, A to F) for the output string. You'll have to bunch the input bits up by 4, left-padding before the '.' and right-padding after it, with 0 in both cases, as needed.
So...:
locate the '.'
left of the '.', bunch by 4, left-padding the last bunch, going leftwards: in your example, 1001 leftmost, then 0110, finally 0001 (left-padding), that's it;
ditto to the right -- in your example 1011, then 1010, then 1010, finally 0110 (right-padding)
each bunch of 4 binary digits, via a hash or other form of hashing, turns into the hex digit to put in that place in the output string.
Want some pseudo-code for it, e.g., Python?
The simplest approach, especially if you already can convert from binary digits to internal numeric representation and from internal numeric representation to hexadecimal digits, is to go binary->internal->hex. I say internal and not decimal, because even though it may print as decimal, it is actually being stored internally in binary format. That said, it is possible to go straight from one to the other. This does not apply to your specific example, but in many cases when converting from binary to hex, you can go four digits at a time, and simply lookup the corresponding hex values in a table. There are all sorts of ways to convert.
BIN to HEX
Binary and hex are natively compatible. Just group 4 binary digits(bits) and substitute the corresponding HEX-digit.
More reference here:
http://en.wikipedia.org/wiki/Hexadecimal#Binary_conversion