Bitwise operator

how does the bitwise operator works???????

Page 1 of 1

4 Replies - 912 Views - Last Post: 02 September 2009 - 02:20 PM Rate Topic: -----

#1 hd_pulse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 09

Bitwise operator

Posted 02 September 2009 - 12:59 PM

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=43,b=76;
printf("%d \n",a & b);
printf("%d \n",a && b);

printf("%d \n",a<<2);
getch();
}
 


when I compile above program i get such outputs...
8
1
172


Plz help me to know how 'bitwise operators' are working here????????


thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Bitwise operator

#2 Guest_Neumann*


Reputation:

Re: Bitwise operator

Posted 02 September 2009 - 01:28 PM

What is there to explain? These are straightforward bitwise operations (althought the second one is just a boolean comparison),

http://www.google.co...lient=firefox-a
Was This Post Helpful? 0

#3 Novast11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 12-June 09

Re: Bitwise operator

Posted 02 September 2009 - 01:36 PM

View Posthd_pulse, on 2 Sep, 2009 - 11:59 AM, said:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=43,b=76;
printf("%d \n",a & b);
printf("%d \n",a && b);

printf("%d \n",a<<2);
getch();
}
 


when I compile above program i get such outputs...
8
1
172


Plz help me to know how 'bitwise operators' are working here????????


thanks


These are binary operations.

43 in binary = 0010 1011
76 in Binary = 0100 1100

For the first print:
printf("%d \n",a & b );


0010 1011
& 0100 1100
-----------------
0000 1000 = 8
Was This Post Helpful? 1
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Bitwise operator

Posted 02 September 2009 - 02:06 PM

There's a few decent tutorials here in the C++ section of </DIC> (here and here)
Was This Post Helpful? 0
  • +
  • -

#5 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Bitwise operator

Posted 02 September 2009 - 02:20 PM

And
a<<2;


This "<<" is called binary left-shift operator it shift the position of the bits from right to left that is in you case 'a' is an int i.e. 32bits but i am assumming it 16bits to make it simple
so 'a' = 43 in binary its 0000000000101011 = 43
now shift it once 0000000001010110 = 86 (43*2)
now shift it again 0000000010101100 = 172 (86*2)
Look at the position of the bits
this just multiply the number by 2 and the number of times we ask it to do ,in your case it was 2 times (a<<2).

And their are many tutorial for these ,their are not too many binary operators in C++ but their are many out comes by those operators and this is logic.

Good luck

This post has been edited by debjit625: 02 September 2009 - 02:23 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1