You are not logged in.

Pradeep.C

Beginner

  • "Pradeep.C" started this thread

Posts: 2

Occupation: trainee

  • Send private message

1

Thursday, August 18th 2005, 10:54am

bit wise & operator

Hi ,

When using & operator i m getting wrong answer

my code is..

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main ()
{

	c = 11000000&11100000;
	
                 for(int i = 7 ; i>=0; i--)
	{
		 if( c & 1<<i)
		       printf("1");
		  else
		       printf("0");
	}
 return 0;
}


my expected result is 11000000

How can i get a correct result?
------------
Pradeep

2

Thursday, August 18th 2005, 12:22pm

You are ANDing decimals 11000000 and 11100000, not the binary numbers you were probably meaning. C++ (and C as well) do not have a easy way to assign values provided in binary notation. You could roll your own function to convert a string of 1's and 0's to an actual number for example. It would look like this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
unsigned int bin(const char *s)
{
   unsigned int r = 0;

   while(*s) {
           r <<= 1;
           r += (*s) - '0';
           s++;
    }

    return r;
}

AP.Sakkthivel

Professional

  • "AP.Sakkthivel" is male

Posts: 563

Location: India

Occupation: Application Development Using Qt

  • Send private message

3

Friday, August 19th 2005, 5:16am

pl try this code;

it behaves very strange for me.

any guess!!

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream.h>
#include <conio.h>
int main()
{
    char c = 00000111&00000111;
    if( c == 00000111)
    cout<< "Success";
    else
    cout<<"Failure";
    c = 00001111&00001111;
    if( c == 00001111)
    cout<< "Success";
    else
    cout<<"Failure";
    getch();
    return 0;
}



Thanks in advance..
Regards
- $akthi

You never know, how soon it may be too lateā€¦

This post has been edited 1 times, last edit by "AP.Sakkthivel" (Aug 19th 2005, 6:30am)


Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

4

Tuesday, August 23rd 2005, 2:01pm

Numerical values starting with 0 are treated as octal numbers by C and C++ compilers

So 00000111 is 8^3+8^2+1 in decimal

You actually want this
c = 0x5 & 0x5

Cheers,
_
Qt/KDE Developer
Debian User

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Tuesday, August 23rd 2005, 2:15pm

Quoted

Originally posted by anda_skoa

c = 0x5 & 0x5


Don't you mean c=0x7 & 0x7? ;) (111b = 1+2+4 = 7)

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

6

Tuesday, August 23rd 2005, 5:38pm

True!

But as the example checks for exactly the same bit pattern in the result it really doesn't matter which pattern is used :)

Cheers,
_
Qt/KDE Developer
Debian User

7

Thursday, August 25th 2005, 9:30am

01. #include <iostream.h>
02. #include <conio.h>
03. int main()
04. {
05. char c = 00000111&00000111;
06. if( c == 00000111)
07. cout<< "Success";
08. else
09. cout<<"Failure";
10. c = 00001111&00001111;
11. if( c == 00001111)
12. cout<< "Success";
13. else
14. cout<<"Failure";
15. getch();
16. return 0;
17. }

the above code written by you is absolutely correct except for one thing ...
in line number 10 the value of the expression "00001111&00001111" will be "00001111". But this value is outside the range of the character variable "c" in which u r storing the result. A char can store the values as ...

signed char -128 to 127
unsigned char 0 to 128

so in the above case the result will wrap aroung to "73" when it will be saved in the variable "c" and the comparison in line 11 will be false. hence the behaviour shown by the above program.

regards
nishant

8

Thursday, August 25th 2005, 9:31am

01. #include <iostream.h>
02. #include <conio.h>
03. int main()
04. {
05. char c = 00000111&00000111;
06. if( c == 00000111)
07. cout<< "Success";
08. else
09. cout<<"Failure";
10. c = 00001111&00001111;
11. if( c == 00001111)
12. cout<< "Success";
13. else
14. cout<<"Failure";
15. getch();
16. return 0;
17. }

the above code written by you is absolutely correct except for one thing ...
in line number 10 the value of the expression "00001111&00001111" will be "00001111". But this value is outside the range of the character variable "c" in which u r storing the result. A char can store the values as ...

signed char -128 to 127
unsigned char 0 to 255

so in the above case the result will wrap aroung to "73" when it will be saved in the variable "c" and the comparison in line 11 will be false. hence the behaviour shown by the above program.

regards
nishant