Using binary operators!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Using binary operators!

 
0
  #1
Feb 1st, 2005
Hello ladies and gents,

I'm starting on the next exercise, but I'm puzzled a bit and was wondering if any of you might help me with this.

I have to enter four integer numbers, non negative and smaller then 16 into ONE variable int x, now, the thing I was wondering is, do I have to use an array for this int x, so that I get int x[3] put the four numbers smaller then 16 into the array or is it possible to solve this solely by using a certain bit operator?

The idea is, when those four numbers are entered, I then have to select a number from 0 to 3 and depending on the selection it has to give me the previous entered number smaller then 16!

Hope you understood what I'm trying to accomplish.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Using binary operators!

 
0
  #2
Feb 1st, 2005
Play around with this and see what you can come up with:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define bit(x) (1UL << (x))
  6.  
  7. int main()
  8. {
  9. unsigned int x = 0;
  10.  
  11. x = 1; // 00000001
  12. cout<< x <<endl;
  13. x |= bit(1) | 1; // 00000011
  14. cout<< x <<endl;
  15. x |= bit(2) | 1; // 00000111
  16. cout<< x <<endl;
  17.  
  18. // Get back a sliver
  19. cout<< (x >> 1) <<endl; // 00000011
  20. cout<< (x >> 2 << 2) <<endl; 00000100
  21. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Using binary operators!

 
0
  #3
Feb 1st, 2005
I will

Thanks Narue!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Using binary operators!

 
0
  #4
Feb 2nd, 2005
Hi Narue,

Ive been playing around with the code you gave me and alltough I'm pretty sure I understand what is happening, I can't really figure out how this is going to help me in getting four different numbers smaller then 16 into one variable x :o

I understand that when having four bits 1111, I get a decimal maximum of 15.

I understand that by using those binary operators << or >> you can shift the binary to the left or right adding a ZERO to the four bits and changing the decimal number.

I understand that using the code x = bit(i) | 1; am putting the bit (i) to 1 and changing the decimal number again.

Have a few question about it also

  1. #define bit(x) (1UL << (x))
  2.  
  3. int main()
  4. {
  5. unsigned int x=0;
  6.  
  7. x |= bit(0) | 1; // 00000001 = 1
  8. cout<< x <<endl;
  9. x |= bit(1) | 1; // 00000011 = 3
  10. cout<< x <<endl;
  11. cout<< (x >> 1 << 1) <<endl; // 00000010 = 2
  12. x |= bit(2) | 1; // 00000111 = 7
  13. cout<< x <<endl;
  14. x |= bit(6) | 1; // 01000111 = 71
  15. cout<< x <<endl;
  16.  
  17. // Get back a sliver
  18. cout<< (x >> 1) <<endl; // 00100011 = 35
  19. cout<< (x >> 2 << 2) <<endl; // 01000100 = 68
  20. cout<< (x >> 1) <<endl; // 00100011 = 35
  21. cout<< (x >> 1 << 1) <<endl; // 01000110 = 70
  22. cout<< (x >> 2 << 1) <<endl; // 00100010 = 34
  23.  
  24. return 0;
  25. }

1) #define bit(x) (1UL << (x)): what does the UL stand for and what does it do? Unsigned Long?

Also, when I deleted UL and left (1 << (x)) it worked aswell, with this the binary operator makes it shift one place to the left.

2) Tough I understand what happens when using these binary operators, I can't really figure out how this is going to help me getting those four decimal numbers into a variable x
Not only that, but when they are entered, I have to retrieve one of them using a number from 0 to 3, so, when I have decimal numbers entered like:
3
7
14
6

And I enter for example 1, it should give me 7 as result, sorry, but I don't see how this could be done with the example code you gave me :o

If you could give me any further hints, I would greatly appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using binary operators!

 
0
  #5
Feb 2nd, 2005
Each nibble takes up 4 bits. So shifting by 4 times the desired position would get you to the desired spot.
  1. #include <stdio.h>
  2.  
  3. unsigned int set(unsigned char nibble, int pos)
  4. {
  5. return nibble << ( pos * 4 );
  6. }
  7.  
  8. unsigned char get(unsigned int value, int pos)
  9. {
  10. return ( value >> ( pos * 4 ) ) & 0xF;
  11. }
  12.  
  13. int main ( void )
  14. {
  15. unsigned int x = set(3,0) + set(7,1) + set(14,2) + set(6,3);
  16. int i;
  17. for ( i = 0; i < 4; ++i )
  18. {
  19. printf ( "get(%d) = %d\n", i, get(x,i) );
  20. }
  21. printf ( "x = %X\n", x );
  22. return 0;
  23. }
  24.  
  25. /* my output
  26. get(0) = 3
  27. get(1) = 7
  28. get(2) = 14
  29. get(3) = 6
  30. x = 6E73
  31. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Using binary operators!

 
0
  #6
Feb 2nd, 2005
Thanks for the help Dave, but, can you explain this to me without using those two functions, it's an exercise in wich functions an references aren't seen yet and to be honest, I'd rather do this exercise without them, it's to confusing for me at this moment

So, how could I do that without using those functions?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using binary operators!

 
0
  #7
Feb 2nd, 2005
Originally Posted by JoBe
Thanks for the help Dave, but, can you explain this to me without using those two functions, it's an exercise in wich functions an references aren't seen yet and to be honest, I'd rather do this exercise without them, it's to confusing for me at this moment

So, how could I do that without using those functions?
I feel like I'm stepping on Narue's toes already, but...
#include <stdio.h>

int main ( void )
{
   unsigned int   x;
   unsigned char  nibble;
   /*
    * Set the value 3 in the 0th nibble.
    */
   x = 3 << (0 * 4);
   printf ( "x = %04X\n", x );
   /*
    * Add in the value 7 in the 1st nibble.
    */
   x += 7 << (1 * 4);
   printf ( "x = %04X\n", x );
   /*
    * Add in the value 14 in the 2nd nibble.
    */
   x += 14 << (2 * 4);
   printf ( "x = %04X\n", x );
   /*
    * Add in the value 6 in the 3rd nibble.
    */
   x += 6 << (3 * 4);
   printf ( "x = %04X\n", x );
   /*
    * Get the value in the 1st nibble.
    */
   nibble = ( x >> (1 * 4) ) & 0xF;
   printf ( "nibble = %d\n", nibble );
   return 0;
}

/* my output
x = 0003
x = 0073
x = 0E73
x = 6E73
nibble = 7
*/
The amount of left or right shift is some parenthesized multiple of 4, which is enough bits for each nibble value.

The bits could be ORed in (|) instead of adding.

For extraction, the & 0xF masks off the lowest 4 bits, after the appropriate shift, and clears any others.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Using binary operators!

 
0
  #8
Feb 2nd, 2005
Thanks,

But why does the nibble need to be a char and can't it be an integer?

Can you explain how this can be summorized: printf ( "x = %04X\n", x );

Using cout, can I write it like this: cout<< "x = " <<hex<<x<<endl; ????

Can I compare putting the numbers 3, 7, 14, 6 into a binary operator as a sort of array?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using binary operators!

 
0
  #9
Feb 2nd, 2005
>But why does the nibble need to be a char and can't it be an integer?

It doesn't need to be a char. [rhetorical]But why waste a full int for 4 bits?[/rhetorical]

>Can you explain how this can be summorized: printf ( "x = %04X\n", x );
>Using cout, can I write it like this: cout<< "x = " <<hex<<x<<endl; ????

Yup.

>Can I compare putting the numbers 3, 7, 14, 6 into a binary operator as a sort of array?

I suppose you could think of the int as an array of nibbles.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Using binary operators!

 
0
  #10
Feb 17th, 2005
Nope, can't figure it out

  1. int main()
  2. {
  3. unsigned int x, j;
  4. unsigned char nibble;
  5.  
  6. for (int i=0;i<4;i++)
  7. {
  8. cin>>j;cin.get();
  9. x+= j << (i * 4);
  10. }
  11.  
  12. for (i=0;i<4;i++)
  13. cout<< hex << nibble = x << (i * 4) <<endl;
  14.  
  15. return 0;
  16.  
  17. }

What I want to do is, is enter four different numbers smaller then 16 and then just print them!

One error that I keep getting is: error C2297: '<<' : illegal, right operand has type 'class ostream &(__cdecl *)(class ostream &)'

This has to do with the last loop, but don't know how to solve it, could someone please help me.

Thank you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC