how many bits correspond to N hexadecimal digits? - binary

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.

Related

What decimal value does the 8-bit binary number 11011111 have if it is on a computer using signed-magnitude representation?

I am confused on how to present this in decimal value
Would it be just the negative value of 223 or would it be -125?
Would it be just the negative value of 223 or would it be -125?
Neither. Signed magnitude representation reserves one bit to represent the sign, and the remaining bits make up the value.
In this case:
bits 0-6 are 1011111, which is 95 in decimal
bit 7 is 1, meaning the value is negative
So the result is -95.
As for your calculation of -125, it appears you got that backwards. 125 is 1111101 in binary. Definitely got your wires crossed there.

Is this just a coincidence that 0xaaaaaaaa represents binary with even positions set as 1

Is this just a coincidence that hexadecimal 0xaaaaaaaa represents binary with even positions set as 1.
Similarly something as elegant as 0x55555555 represents binary with odd positions set as 1 ?
Binary representation of 5 is 0101. So 0X55555555 has 16 ones, 16 zeros and the ones,zeros take alternate positions. Similarly 0X33333333 has 16 ones, 16 zeros and 2 consecutive ones, 2 consecutive zeros alternate.
Nothing special about those numbers per se, other than the fact that their corresponding bit patterns are useful.
I think the key realization here is that it's super easy to come up with a compact hex number to represent any longer bit pattern (even easier if it's repeating), right off the top of your head.
Why? Because it's trivial to convert from hex-to-binary or binary-to-hex - every four bits of the pattern can be neatly represented by one hex digit:
So let's say I wanted this 16-bit mask: 1110111011101110. This is 1110 repeated 4 times, so it's just some hex digit, 4 times. Since 1110 is 14 in decimal, that's gonna be "E", so our mask would be: 0xEEEE.

java byte to binary conversion

Based on my statements below I have few questions. Please explain.
Q1. Why the output is same for items 2 and 4. I guess it is because byte 128 is equal to byte -128
Q2. Why the output for items 2 and 4 are padded with 1s on the left where the output is supposed to be just this 10000000. I guess.
Q3. What is the difference between outputs 2 and 3 even though the last out 8 bits looks same.
1. System.out.println("1==>"+Integer.toBinaryString((byte)127));
2. System.out.println("2==>"+Integer.toBinaryString((byte)128));
3. System.out.println("3==>"+Integer.toBinaryString(128));
4. System.out.println("4==>"+Integer.toBinaryString((byte)-128));
output :
1==>1111111
2==>11111111111111111111111110000000
3==>10000000
4==>11111111111111111111111110000000
A byte in Java is actually a signed 8-bit integer. It can only represent numbers from -128 to 127.
Q1. 128 is an overflow. It becomes -128.
Q2. Both these are negative numbers. For negative numbers, the method returns "the argument plus 2^32". This results in a lot of leading 1s. For positive numbers, the output just omits the leading 0.
Q3. In example 3, you don't use a byte. You use int. This means you can represent 128, it is not an overflow.

Converting decimal / binary to sign and magnitude,

I am researching how to convert a decimal number to a sign a magnitude number,
I understand that the number 19 in binary equates to 10011 as a binary string, however now i am stuck on converting this to a sign and magnitude number.
From my understanding i would say that 10011 would equal -3 in sign and magnitude because of the sign 'most significant' digit in the binary string being a 1 meaning that it must be negative, and left over is the 0011 which converting it back to decimal is 3... so am i right that 19 in decimal is -3 is sign and magnitude ? or have i got something wrong ? i can't find any place only which fully explains how this process works.
Thankyou for any help.
I get the feeling that I am doing your homework but you don't understand so I will try to explain.
The decimal number is 19. You convert that to binary and it is 10011. But what if you want to convert -19? You can't represent that currently because there is no "-" in binary. So you decide to put an extra bit in the front of your binary number to represent whether it is a positive or negative. The most significant digit is 1 so you say that a 1 is going to represent a negative and a 0 is going to represent a positive. So this time when you convert 19 you get 010011. The first digit is a 0 so you know that it is positive. The first digit is the sign. When you convert -19 you get 110011. The first digit is a 1 so you know that it is a negative. The first digit is the sign. The rest of the number indicates the magnitude. In both of those two sums the magnitude is 19 but the sign is different.
If you convert decimal (19) to binary (10011) to decimal again you need to get back to the original answer and 19 is not -3.
Hope this helps.
Watch this https://www.youtube.com/watch?v=ikThX9Z0jUo

Decimal/Hexadecimal/Binary Conversion

Right now I'm preparing for my AP Computer Science exam, and I need some help understanding how to convert between decimal, hexadecimal, and binary values by hand. The book that I'm using (Barron's) includes an example but does not explain it very well.
What are the formulas that one should use for conversion between these number types?
Are you happy that you understand number bases? If not, then you will need to read up on this or you'll just be blindly following some rules.
Plenty of books would spend a whole chapter or more on this...
Binary is base 2, Decimal is base 10, Hexadecimal is base 16.
So Binary uses digits 0 and 1, Decimal uses 0-9, Hexadecimal uses 0-9 and then we run out so we use A-F as well.
So the position of a decimal digit indicates units, tens, hundreds, thousands... these are the "powers of 10"
The position of a binary digit indicates units, 2s, 4s, 8s, 16s, 32s...the powers of 2
The position of hex digits indicates units, 16s, 256s...the powers of 16
For binary to decimal, add up each 1 multiplied by its 'power', so working from right to left:
1001 binary = 1*1 + 0*2 + 0*4 + 1*8 = 9 decimal
For binary to hex, you can either work it out the total number in decimal and then convert to hex, or you can convert each 4-bit sequence into a single hex digit:
1101 binary = 13 decimal = D hex
1111 0001 binary = F1 hex
For hex to binary, reverse the previous example - it's not too bad to do in your head because you just need to work out which of 8,4,2,1 you need to add up to get the desired value.
For decimal to binary, it's more of a long division problem - find the biggest power of 2 smaller than your input, set the corresponding binary bit to 1, and subtract that power of 2 from the original decimal number. Repeat until you have zero left.
E.g. for 87:
the highest power of two there is 1,2,4,8,16,32,64!
64 is 2^6 so we set the relevant bit to 1 in our result: 1000000
87 - 64 = 23
the next highest power of 2 smaller than 23 is 16, so set the bit: 1010000
repeat for 4,2,1
final result 1010111 binary
i.e. 64+16+4+2+1 = 87 in decimal
For hex to decimal, it's like binary to decimal, only you multiply by 1,16,256... instead of 1,2,4,8...
For decimal to hex, it's like decimal to binary, only you are looking for powers of 16, not 2. This is the hardest one to do manually.
This is a very fundamental question, whose detailed answer, on an entry level could very well be a couple of pages. Try to google it :-)