•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,790 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,767 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 6135 | Replies: 33
![]() |
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.
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.
Play around with this and see what you can come up with:
#include <iostream>
using namespace std;
#define bit(x) (1UL << (x))
int main()
{
unsigned int x = 0;
x = 1; // 00000001
cout<< x <<endl;
x |= bit(1) | 1; // 00000011
cout<< x <<endl;
x |= bit(2) | 1; // 00000111
cout<< x <<endl;
// Get back a sliver
cout<< (x >> 1) <<endl; // 00000011
cout<< (x >> 2 << 2) <<endl; 00000100
} I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
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)): 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.
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
#define bit(x) (1UL << (x))
int main()
{
unsigned int x=0;
x |= bit(0) | 1; // 00000001 = 1
cout<< x <<endl;
x |= bit(1) | 1; // 00000011 = 3
cout<< x <<endl;
cout<< (x >> 1 << 1) <<endl; // 00000010 = 2
x |= bit(2) | 1; // 00000111 = 7
cout<< x <<endl;
x |= bit(6) | 1; // 01000111 = 71
cout<< x <<endl;
// Get back a sliver
cout<< (x >> 1) <<endl; // 00100011 = 35
cout<< (x >> 2 << 2) <<endl; // 01000100 = 68
cout<< (x >> 1) <<endl; // 00100011 = 35
cout<< (x >> 1 << 1) <<endl; // 01000110 = 70
cout<< (x >> 2 << 1) <<endl; // 00100010 = 34
return 0;
}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.
Each nibble takes up 4 bits. So shifting by 4 times the desired position would get you to the desired spot.
#include <stdio.h>
unsigned int set(unsigned char nibble, int pos)
{
return nibble << ( pos * 4 );
}
unsigned char get(unsigned int value, int pos)
{
return ( value >> ( pos * 4 ) ) & 0xF;
}
int main ( void )
{
unsigned int x = set(3,0) + set(7,1) + set(14,2) + set(6,3);
int i;
for ( i = 0; i < 4; ++i )
{
printf ( "get(%d) = %d\n", i, get(x,i) );
}
printf ( "x = %X\n", x );
return 0;
}
/* my output
get(0) = 3
get(1) = 7
get(2) = 14
get(3) = 6
x = 6E73
*/ 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
So, how could I do that without using those functions?
Thanks
•
•
•
•
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?
#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 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.
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?
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?
>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.
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.
Nope, can't figure it out
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!
int main()
{
unsigned int x, j;
unsigned char nibble;
for (int i=0;i<4;i++)
{
cin>>j;cin.get();
x+= j << (i * 4);
}
for (i=0;i<4;i++)
cout<< hex << nibble = x << (i * 4) <<endl;
return 0;
}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!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
Other Threads in the C Forum
- Previous Thread: segmentation fault
- Next Thread: multiple file searching



Linear Mode