944,052 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8858
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 1st, 2005
0

Using binary operators!

Expand Post »
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.
Similar Threads
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Feb 1st, 2005
0

Re: Using binary operators!

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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 1st, 2005
0

Re: Using binary operators!

I will

Thanks Narue!
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Feb 2nd, 2005
0

Re: Using binary operators!

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.
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Feb 2nd, 2005
0

Re: Using binary operators!

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. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 2nd, 2005
0

Re: Using binary operators!

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
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Feb 2nd, 2005
0

Re: Using binary operators!

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 2nd, 2005
0

Re: Using binary operators!

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?
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Feb 2nd, 2005
0

Re: Using binary operators!

>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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 17th, 2005
0

Re: Using binary operators!

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!
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: summation
Next Thread in C Forum Timeline: Text Fragment Extractor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC