Hi, I am trying to do a NOT operation but it doesnt seem to work well: unsigned int i=8; printf("%d \n", ~i); This is suppose to print 247 but I get -9 Any ideas of what am I doing wrong ? BTW, I am just practicing on playing with bits.. Thanks, Raul
On Mon, 2 Jun 2003, Raúl Gutiérrez Segalés wrote:
Hi,
I am trying to do a NOT operation but it doesnt seem to work well:
Sorry I meant 1's complement.
unsigned int i=8;
printf("%d \n", ~i);
This is suppose to print 247 but I get -9
Any ideas of what am I doing wrong ?
BTW, I am just practicing on playing with bits..
Thanks, Raul
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
On Mon, 2 Jun 2003, Raúl Gutiérrez Segalés wrote:
Hi,
I am trying to do a NOT operation but it doesnt seem to work well:
Sorry I meant 1's complement.
Actually I am still reading Jerry's function (getone()) and my trying to , breaking my head, understand this two assignations: newflags.c_cflag |= (CLOCAL | CREAD); newflags.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); The first one is very obvious since it is a simple oring which just adds the options but second one has an AND but first it does a 1's complement so I cant actually see the logic behind it.. Any one has mercy of this poor learner ?? :) Thanks once again, Raul
unsigned int i=8;
printf("%d \n", ~i);
This is suppose to print 247 but I get -9
Any ideas of what am I doing wrong ?
BTW, I am just practicing on playing with bits..
Thanks, Raul
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
On 2 Jun 2003 at 1:43, Raúl Gutiérrez Segalés wrote: Date sent: Mon, 2 Jun 2003 01:43:32 -0400 (PYT) From: Raúl Gutiérrez Segalés <rau@campoalto.edu.py> To: <suse-programming-e@suse.com> Subject: Re: [suse-programming-e] working with bits
On Mon, 2 Jun 2003, Raúl Gutiérrez Segalés wrote:
Hi,
I am trying to do a NOT operation but it doesnt seem to work well:
Sorry I meant 1's complement.
Actually I am still reading Jerry's function (getone()) and my trying to , breaking my head, understand this two assignations:
newflags.c_cflag |= (CLOCAL | CREAD); newflags.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
The first one is very obvious since it is a simple oring which just adds the options but second one has an AND but first it does a 1's complement so I cant actually see the logic behind it..
Any one has mercy of this poor learner ?? :)
Thanks once again,
Raul
[snip...] It's not as difficult as it looks at first - break it down into two bits: ~(ICANON | ECHO | ECHOE | ISIG) This gives us a mask flag set that has all the bits set to one -except- ICANON, ECHO, ECHOE and ISIG, which are set to zero. OK, we now and (&) our mask flag set with the target set. Remember with anding we need both target and mask set bits set to one for the flag to be set to one in the result. This means that ICANON, ECHO, ECHOE and ISIG will be set to zero, since we set their bits to zero in the mask. It also means that any other bit in the target that was originally set to one will remain set to one (since both mask and target bits will be set to one) while result bits set to zero int the target will be left at zero (because only the bit in the mask is set to one). So, the net effect is that the bits corresponding to ICANON, ECHO, ECHOE and ISIG are cleared (set to zero) without affecting the settings any of the others bits in the result flag set. Hope that helps <g> alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
Raúl Gutiérrez Segalés <rau@campoalto.edu.py> writes:
Hi,
I am trying to do a NOT operation but it doesnt seem to work well:
unsigned int i=8;
printf("%d \n", ~i);
This is suppose to print 247 but I get -9
Any ideas of what am I doing wrong ?
Try using printf("%hhu \n", ~i);
Raúl Gutiérrez Segalés wrote: Hi,
I am trying to do a NOT operation but it doesnt seem to work well:
unsigned int i=8;
printf("%d \n", ~i);
This is suppose to print 247 but I get -9
sure? With "unsigned char" I would expect 247 but not with "unsigned int".
Any ideas of what am I doing wrong ?
Try "%u" instead of "%d" in your printf statement. Michael
On Mon, 2 Jun 2003 01:23:51 -0400 (PYT) Raúl Gutiérrez Segalés <rau@campoalto.edu.py> wrote:
Hi,
I am trying to do a NOT operation but it doesnt seem to work well:
unsigned int i=8;
printf("%d \n", ~i);
This is suppose to print 247 but I get -9
Any ideas of what am I doing wrong ?
BTW, I am just practicing on playing with bits..
This has been answered correctly. printf("%u \n", ~i); The %u prints unsigned. You start with 0000 0000 0000 0000 0000 0000 0000 1000 You should end up with 11111111111111111111111111110111. You might understand things better if you use %x also. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
participants (5)
-
alan@ibgames.com
-
Graham Murray
-
Jerry Feldman
-
Michael Schulz
-
Raúl Gutiérrez Segalés