HOW INT’S PERFORM AT BIT LEVEL--
As we discussed in the last blog about the int.But many users request to go more deep to give explanation about how int’s actually perform at bit level.
SO LET’S START-
As we discussed in last blog about range of int’s that using arithmetic modulo 2^n where n is the no. of bits we can find the range it is correct But in computation mainly 2^n-1 uses to find the range of int data types.see how--Let’s take example of char which is also a one form of the int.char have 8 bits and using 2^8 we get 256 characters.but its range is 0 to 255 due to includation of 0 means 2^8-1.that’s why we use 2^n-1 because it is correct in terms of representation.
BUT HOW IT LOOK AT BIT LEVEL--
As we know that char takes 1 byte means 8 bits and at memory level we know that only binary system uses means 0 or 1 and each bit is filled with either 0 or 1.so there are many combinations let’s see the char representation -Here i use '#' to represent the bits block
like this it represent manily each bit contain either 0 or 1 and there are 8 bits so this concludes are modulo 2^8(2^n n= no.of bits).
HOW INT OPERATIONS PERFORM AT BIT LEVEL-
Generally,for basic operations like addition, subtraction etc.perform at bit level with the help of concept "complement".Mainly 2’s complement uses instead of 1’s complement.2’s complement is a mathematical operation on binary numbers or a way to represent integers.It also help to represent the negative numbers or notations to execute.It is equivalent to take 1’s complement and then adding 1 to it.
And generally,we find one’s complement by inverting all given bits like example 3 means 00000011 if we want its 1’s complement then we invert it like this 11111100.
And if add 1 to it then it give 2’s complement of 3
11111100 +1=11111101 which represent -3.
Ranges of both complement-
1’s - -(2^n-1) to +(2^n-1-1)
2’s- -(2^n-1) to +(2^n-1-1)
Sum of no. and its 2’s complement will always equal to 0.while in 1’s complement it will be -0..
@In 2’s complement system has advantage that it discarded overflow from the result..(overflow we discuss in last blog) if i tell this same thing at bit level it looks like this-as long as the inputs are represented in the same no. of bits and any overflow beyond those bits is discarded from result).
2’s complement is mainly use to represent negative value at bit level how and why?
In 2’s complement notation a non-negative number is represented by its ordinary binary representation.In this case the most significant bit is 0.Though the range of numbers represented is not same as with unsigned binary numbers.for ex-an 8 bit unsigned numbers can represent the values 0 to 255(11111111).However a 2’s complement 8 bit number can only represent positive integers from 0 to 127(01111111),because the rest of the bits combinations with most significant bit as '1' represent the negative integers -1 to -128 .The 2’s complement operation is the additive inverse operation,so negative numbers are represented by 2’s complement of absolute value.
CONVERSION OF BITS(HOW DATATYPES DATA CONVERT INTO EACH OTHER )--
It mainly tells about how one data type data store into other data type..
When turning a 2’s complement number with a certain no. of into one with more bits (eg--when copying 1 byte variable to a 2 byte variable),the most significant bit must be repeated in all the extra bits.some processors do this in a single instruction;but on other processor a conditional must be used followed by code to set the relevant bits or bytes.
Let’s take a example -
as we know that 'a' have ascii value 97,firstly ch variable have it’s datatype char means it have 1 byte to represent it how see it-----0/1/0/0/0/0/0/1
when it converts to int which is take mainly 2 or 4 bytes let’s take 2 byte then the 1 byte is represent like as it represent into char and then the other one is empty.so it will fill with the most significant bit which is present in 1st byte see here
1st byte . 0/1/0/0/0/0/0/1
2nd byte. 0/0/0/0/0/0/0/0
here you can easily see that the 1st byte is same as char but the 2nd one is filled with 0’s because as we discussed above that empty spaces fill with most significant bit which is the leftmost bit of 1st byte that is 0.
THIS IS HOW BITS PLAY!!