Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

suppose i have the following program

C++
main()
{
int a;
printf("enter the value of a\n");
scanf("%d\n",&a);

}


Now if i enter number 2 it will be stored as 00000010 in a.I want to get the last two bits(32-bit system) of a only.
How to get that.
Posted
Comments
Andreas Gieriet 9-Jul-14 13:01pm    
You seem to ask some question once in a while. Please consider to also "accept" the answers to respect the efforts of those who cared to answer your questions.
Thanks
Andi

try:
C++
a = a & 3;
 
Share this answer
 
Comments
Robert Clove 9-Jul-14 11:03am    
@OriginalGriff why & 3?
jeron1 9-Jul-14 11:17am    
because it's the bitwise AND operator as opposed to the reference operator.
Legor 10-Jul-14 4:25am    
3 is 0011 in binary representation.
You might need to learn about bit manipulations and binary numbers.
You may experiment first with paper and pencil:
- what is ther binary represntation of say 0, 1, 2, 3, 4, ..., 15?
- what do the the following operators: bit-and (&), bit-or (|), bit-xor (^), complement (~)?
Work with 4 bit values only to start with.

E.g. decimal 3 = binary 0011.

The right most binary digit has the factor 1, the next on the left is 2, then 4 and then 8.
This is analogous to decimal numbers: the right most is factor 1, then 10, then 100, etc.
Decimal: 1 = 100, 10 = 101, 100 = 102, etc.
Binary: 1 = 20, 2 = 21, 4 = 22, etc.

E.g. decimal 123 is 1x102 + 2x101 + 3x100 = 100 + 20 + 3
E.g. binary 1110 is 1x23 + 1x22 + 1x21 + 0x20 = decimal 8+4+2+0 = 14

For bit-operations: the operations are bitwise:
x & y: result bit is 1 if x==1 and y==1, 0 otherwise    (bit-and)
x | y: result bit is 1 if x==1 or  y==1, 0 otherwise    (bit-or)
x ^ y: result bit is 1 if x!=y,          0 otherwise    (bit-xor)
~x:    result bit is 1 if x==0,          0 otherwise    (complement)


E.g. decimal 6 & 3 = binary 0110 & 0011 = 0010.
You might prefer to write it this way:
6 = 0110
3 = 0011
& = 0010   the bits that are both 1 result in 1, all others in 0


So, your question on getting the lowest two bits only:
C
int a;
...
a = a & 3; // 3 = binary ...0011
...

This results in all bits but the lowest two are always 0, and the lowest two bits are taken as-is.

Cheers
Andi
 
Share this answer
 
v4
Comments
nv3 9-Jul-14 13:59pm    
A nice little lesson about the binary system. +5.
Why don't people use Wikipedia to look those things up!
Andreas Gieriet 9-Jul-14 16:33pm    
Thanks for your 5!
What term to look up?
If you know to ask the question, you are usually very close to the answer already ;-)
Cheers
Andi
Maciej Los 9-Jul-14 16:48pm    
Great job, Andi!
+5!
Andreas Gieriet 9-Jul-14 17:01pm    
Thanks for your 5, Maciej!
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900