How is "byte" datatype size = 1 byte while it can have 3 digit numbers? - binary

Data Type: Byte
Size: 1 byte/ 8 bits
Info: An integer between -128 and 127
I found this online, but I was a bit confused, I am new to binary things, sorry if I am wrong, but one single digit is 8 bits, right? So, if that datatype has 3 digits of number, how is its size = 1 byte/ 8bits??
I tried checking in https://www.rapidtables.com which converts text to binary numbers, I put "127" as input and it gave me the output as
00110001 00110010 00110111
which is 24 bits/ 3 bytes..
which makes sense in my brain...
So what does that above information mean by that?

I think you confuse a string number with an integer.
A single byte (8-bits) can represent 256 numbers in the following ranges:
signed: -128 - 127, max: 2 ^ 8 / 2 - 1 (2 ^ 7 - 1)
unsigned: 0 - 255, max: 2 ^ 8 - 1
The website from your link converts a text to binary so you have 3 ASCII characters:
char
dec
hex
bin
'1'
49
0x31
0b110001
'2'
50
0x32
0b110010
'3'
51
0x33
0b110011
References:
Signed number representations

Related

What Does x-bit integer mean?

I have a very basic question related to the data type.
if I have one array of let say 5 elements
x = [3.23 1.47 3.79 8.91 6.01],
and it's data type is 8-bit integer.
Does it mean that each entry in the above array can be represented by 8 binary bits and it can take the values from -128 to 127 OR from 0 .. 255 ? OR something else.
Integer is another word for "whole number." For example: 1 and -17 are integers, while 3.23 and pi are not. Your array is not an array of integers.
There are 256 different possible values for 8 bits. There are two common choices for which 256 numbers to use: unsigned and signed. The unsigned 8 bit integers are 0, 1, 2, ..., 255, and the signed are -128, -127, ..., +127.

Need assistance with Octal number subtraction

So I am trying to understand the difference between unsigned and signed octal number subtraction if the octal numbers were 6 bits. For example, octal 76 - octal 64:
I first convert 76 to binary which becomes 111 110 and 64 to binary which becomes 110 and 100:
But the problem is, if these octal numbers represent signed 6-bit octal numbers, would that mean the 111 110 is negative and 110 100 is also negative, meaning that the subtraction operator will cancel with the negative sign of the second octal number, resulting in an addition? Or do we just treat it normally, subtract the 2 binary numbers normally, and then look at the sign after?
Your signed representation should not matter.
Take for example decimal -1, which in 6-bit octal would be 077 (representing octal with a 0 prefix, 2's complement representation assumed).
Then 077 - 077 = 000 , as expected. In binary, decimal -1 is 111 111. But -(-1) is 1, which is octal 001. 077 - 077 = 077 + 001 = 000, with overflow truncated.
You can also consider base 8 arithmetic directly. 076 - 064 = 012. Instead of borrowing 10, as in decimal, you borrow 8. Consider 012 - 003. You borrow 8 from the 1 of 012 and add it to the 2. 00{8+2} - 003 = 007.
If 076 is signed (assuming 2's complement), then in decimal, it is -2. Similarly, 064 in decimal is -12. The difference is -2 - (-12) = 10, which in octal is 012. No difference, whether signed or not.
The sign representation can be imposed after the arithmetic.

Binary digits in decimal

How do you represent (decimal) integer 50 in binary?
How many bits must be "flipped" in order to capitalize a lowercase 'a' that is represented in ASC11?
How do you represent the (decimal) integer 50 in, oh, "hexadecimal," otherwise known as base-16? Recall that decimal is simply base-10, and binary is simply base-2. Infer from those base systems how to represent this one?
Please answer these questions for me.HELP.
To help you some:
Binary is only made up of 1's and 0's.This may help you understand binary conversion
Decimal is 0-9
Hexadecimal is 0-9, then A-F (so A would represent 10, B would be 11, etc up to F which is 15)
Converting from decimal to another base
Here some tips for you regarding conversion to binary:
What is 50 mod 2? What about 25 mod 2 and then 12 mod 2? What are your results if you continue this?
What does any number mod 2 (always) return as result? - 1 or 0
Do you realise any patterns? - You get the reversed binary number as result
Test case 50:
50 mod 2 = 0 - 6th digit
25 mod 2 = 1 - 5th digit
12 mod 2 = 0 - 4th digit
6 mod 2 = 0 - 3rd digit
3 mod 2 = 1 - 2nd digit
1 mod 2 = 1 - 1st digit
The remainders of the divisions concatenated and reverses are: 110010, which is 50 in binary.
Can this be also transformed to further bases? - Yes, as we see with trying to convert 50 to hexadecimal:
50 mod 16 = 2 - 2nd digit
3 mod 16 = 3 - 1st digit
The remainders again concatenated and reversed are 32, which conveniently is 50 in hexadecimal.
In general we can say to convert a number to an arbitrary base you must take the remainder of the number and the base and then divide the number by the base and do the same thing again. In a program this would look something like:
while the number is greater 0 do:
result = (number mod base) + result;
number = number div base;
Converting from any base to decimal
How do you convert a number from an arbitrary base into base 10? First let us do a test case with binary. Lets take the 50 from the previous example: 110010
The method to convert from binary is multiplying every digit with the base to the power of the position of it in the number and adding up the result. The enumeration of the positions begins with 0 at the least significant digit. Our previous number would then look something like this:
1 *2^5 + 1 *2^4 + 0 *2^3 + 0 *2^2 + 1 *2^1 + 0 *2^0
What simplifies to:
32 + 16 + 2 = 50
It also works with any other base, like our 32 from the previous example:
3 *16^1 + 2*16^0 = 48 + 2 = 50
In program this would look something like this:
from end of number to beginning do:
result = result + digit * (base ^ position)

The binary equivalent of the decimal number 104

Ok,so I know that the binary equivalent of 104 is 1101000.
10=1010
4=0100
so , 104=1101000 (how to get this??how these both mix together and get this binary?)
And from the example here...
the octets from "hellohello" are E8 32 9B FD 46 97 D9 EC 37.
This bit is inserted to the left which yields 1 + 1101000 = 11101000 ("E8").
I still understand this part , but how to convert 11101000 to E8?
I'm so sorry for all these noob questions , I just learn it yesterday , I googled and search for a whole day but still not really understand the concept...
Thank you.
Ok,so I know that the binary equivalent of 104 is 1101000.
10=1010
4=0100
You can't break apart a number like 104 into 10 and 4 when changing bases. You need to look at the number 104 in its entirety. Start with a table of bit positions and their decimal equivalents:
1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000
Look up the largest decimal number that is still smaller than your input number: 104 -- it is 64. Write that down:
1000000
Subtract 64 from 104: 104-64=40. Repeat the table lookup with 40 (32 in this case), and write down the corresponding bit pattern below the first one -- aligning the lowest-bit on the furthest right:
1000000
100000
Repeat with 40-32=8:
1000000
100000
1000
Since there's nothing left over after the 8, you're finished here. Sum those three numbers:
1101000
That's the binary representation of 104.
To convert 1101000 into hexadecimal we can use a little trick, very similar to your attempt to use 10 and 4, to build the hex version from the binary version without much work -- look at groups of four bits at a time. This trick works because four bits of base 2 representation completely represent the range of options of base 16 representations:
Bin Dec Hex
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F
The first group of four bits, (insert enough leading 0 to pad it to four
bits) 0110 is 6 decimal, 6 hex; the second group of four bits, 1000 is
8 decimal, 8 hexadecimal, so 0x68 is the hex representation of 104.
I think you are making some confusions:
104 decimal is 1101000 which is not formed by two groups splitting 104 into 10 and 4.
The exception is for hex numbers that can be formed by two groups 4 binary numbers (2^4 = 16).
So 111010000 = E8 translates into 1110 = E and 8 = 10000. 1110 (binary) would be 14 (decimal) and equivalent of E (hex).
Hex numbers go from 0 to 15 (decimal) where:
10 (decimal) = A (hex)
11(decimal) = B(hex)
...
15(decimal) = F(hex)
What you're missing here is the general formula for digital numbers.
104 = 1*10^2 + 0*10^1 + 4*10^0
Similarly,
0100b = 0*2^3 + 1*2^2 + 0*2^1 + 0*0^0
And for a hexidecimal number, the letters A-F stand for the numbers 10-15. So,
E8 = 14*16^1 + 8*16^0
As you go from right to left, each digit represents the coefficient of the next higher power of the base (also called the radix).
In programming, if you have an integer value (in the internal format of the computer, probably binary, but it isn't relevant), you can extract the right most digit with the modulus operation.
x = 104
x % 10 #yields 4, the "ones" place
And then you can get "all but" the rightmost digit with integer division (integer division discards the remainder which we no longer need).
x = x / 10 #yields 10
x % 10 #now yields 0, the "tens" place
x = x / 10 #yields 1
x % 10 #now yields 1, the "hundreds" place
So if you do modulus and integer division in a loop (stopping when x == 0), you can output a number in any base.
This is basic arithmetic. See binary numeral system & radix wikipedia entries.

What is the "biggest" negative number on a 4-bit machine?

Or, what is the range of numbers that can be represented on a 4-bit machine using 2s-complement?
That would be -8 to +7
The range is -8 to 7, or 1000 to 0111. You can see the full range here.
4 bits (using 2's complement) will give you a range from -8 to 7.
This should be straightforward to work out yourself.
Range in twos complement will be:
-1 * 2 ^ (bits - 1)
to
2 ^ (bits - 1) - 1
So for 4 bits:
-1 * 2 ^ (4 - 1) = -1 * 2 ^ 3 = -8
to
2 ^ (4 - 1) - 1 = 2 ^ 3 - 1 = 7
Also, if you are interested and for others maybe browsing this question -
twos complement is used for easy binary arithmetic:
to add - you just add the two numbers without conversion and disregard the overflow:
-6 + 7 = 1
is
1010 = -6
0111 = 7
------
(1)0001 = 1 (ignoring the overflow)
...and more yet - to convert a negative binary number to its opposite positive number:
if the sign bit (highest order bit) is 1, indicating negative... reading from least significant to most significant bit (right to left), preserve every bit up through the first "1", then invert every bit after that.
So, with 8 bit
10011000 .. becomes
01101000 (* -1) = 104 * -1 = -104
and that is why 10000000 is your lowest negative number (or in X bit 1000.all zeroes..000), it translates to unsigned 10000000 * -1 = -128
Maybe a long winded answer but to those without the 1s and 0s background I figure it is useful
Well let's dissect this question.
Firstly, the question should be framed as - "The least*(because it is negative, so biggest is not the right usage)* possible negative number to be stored in 4-bit would be?"
Numbers are of two types -
Signed (holds both + and - numbers )
Unsigned (holds only + numbers)
We will be using binary representation to understand this.
For Unsigned -
4-bit minimum number = 0000 (0)
4-bit maximum number = 1111 (255)
So range would be Range : 0-15
For Signed 4-bits, first bit represent sign(+/-) and rest 3-bits represent number.
4-bit minimum will be a negative number.
So replace the first bit(MSB) with 1 in 0000(least unsigned number), making it 1000.
Calculate decimal equivalent of 1000 = 1*2^3 = 8
So, number would be -8 (- as we have 1 as the first bit in 1000)
4-bit maximum will be a positive number.
So replace the first bit(MSB) with 0 in 1111(largest unsigned number), making it 0111.
Calculate decimal equivalent of 0111 = 1*2^2 + 1*2^1 + 1*2^0 = 7
So, number would be +7 (+ as we have 0 as the first bit in 0111)
Range would be -8 to +7.