Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i recently learnt about bitwise operators and it says if you did:
int num = 16;
int newnum = num >> 2;

the value of 16 in binary is 1000, so the result of doing int newnum = num >> 2; would be 00100. This is what i dont understand. Shouldnt the value become 0010, since doing int newnum = num >> 1; would be 0100?

What I have tried:

ive tried searching it up but i dont find anything
Posted
Updated 15-Apr-21 21:55pm
Comments
CPallini 16-Apr-21 3:19am    
"the value of 16 in binary is 1000"
Nope: as correctly pointed out by markkuk,
1000 binary <=> 8 decimal

This might be helpful :
int num  = 16 
         = 0001 0000

num << 1 = 0010 0000
num << 2 = 0100 0000
num << 3 = 1000 0000

num >> 1 = 0000 1000
num >> 2 = 0000 0100
num >> 3 = 0000 0010
 
Share this answer
 
16 decimal is 10000 binary. 16 >> 2 == 4, which is 100 binary.
 
Share this answer
 
Comments
CPallini 16-Apr-21 3:17am    
5

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