[chbot] Pop quiz C

hamster hamster at snap.net.nz
Wed Apr 26 00:14:40 BST 2017


 

Yes, an odorous rodent, with an itch head. 

Part 2: 

$ cat
check.c
#include <stdio.h> 

int main(int argc, char *argv[])
{
 /*
unsigned int is 32 bits */
 unsigned int a,b;
 unsigned int n0 = 24, n1
= 10; 

 a = 0xFFFFFFFF;
 a = a << (n0+n1); 

 b = 0xFFFFFFFF;
 b = (b
<< n0) << n1; 

 printf("%08X should equal %08Xn", a, b);
 return 0;
}


Both 'a' and 'b' are shifted right by a total of 34 bits. 

$ gcc -o
check check.c -Wall -pedantic
$ ./check
FFFFFFFC should equal 00000000


It gives different answers depending on optimizations, and no
warnings! 

$ gcc -o check check.c -Wall -pedantic -O4
$
./check
00000000 should equal 00000000
$ 

After 80286, Intel behavior
is to mask the shift count by 0x1F, for 32-bit values. 

Mike 

On
26.04.2017 11:00, Charles Manning wrote: 

> It depends.... the ultimate
weasel answer :-).
> 
> I get a compilation error because I always
compile with -Wall -Werror. SInce everyone should be doing that (my
decree) that is the ultimate answer.
> 
> gcc bigshift.c -o bigshift
-Wall -Werror
> bigshift.c: In function 'main':
> bigshift.c:5:29:
error: left shift count >= width of type
[-Werror=shift-count-overflow]
> printf("%dn", 0xFFFFFFFF << 34);
> ^
>
cc1: all warnings being treated as errors
> 
> On Wed, Apr 26, 2017 at
10:52 AM, Mark Atherton <markaren1 at xtra.co.nz> wrote:
> 
>> Doesn't it
depend on what 'a' is declared as, and the language ?
>> 
>> Assuming C,
and if 'a' was a U32, and 0xFFFFFFFF was 0xFFFFFFFFL, and 34 was 34L,
then possibly the answer might, perhaps, possibly be 0.
>> 
>> having
said that, given the context of the question and the person asking the
question, I smell an odorous rodent :)
>> 
>> -Mark
>> 
>>
http://www.c4learn.com/c-programming/c-bitwise-left-shift-operator/
[4]ties up with my understanding, but again I may need more
anti-paranoia medication... 
>> 
>>> On 26 April 2017 at 10:04 hamster
<hamster at snap.net.nz> wrote:
>>> 
>>> Sorry - no chocolate fish this
time 
>>> 
>>> What is the value of a? 
>>> 
>>> a = 0xFFFFFFFF << 34;

>>> 
>>> Have you checked your answer?
>> 
>>>
_______________________________________________
>>> Chchrobotics mailing
list Chchrobotics at lists.linuxnut.co.nz
>>>
http://lists.ourshack.com/mailman/listinfo/chchrobotics [1]
>>> Mail
Archives: http://lists.ourshack.com/pipermail/chchrobotics/ [2]
>>>
Meetings usually 3rd Monday each month. See http://kiwibots.org [3] for
venue, directions and dates.
>>> When replying, please edit your Subject
line to reflect new subjects.
>> 
>>
_______________________________________________
>> Chchrobotics mailing
list Chchrobotics at lists.linuxnut.co.nz
>>
http://lists.ourshack.com/mailman/listinfo/chchrobotics [1]
>> Mail
Archives: http://lists.ourshack.com/pipermail/chchrobotics/ [2]
>>
Meetings usually 3rd Monday each month. See http://kiwibots.org [3] for
venue, directions and dates.
>> When replying, please edit your Subject
line to reflect new subjects.

 

Links:
------
[1]
http://lists.ourshack.com/mailman/listinfo/chchrobotics
[2]
http://lists.ourshack.com/pipermail/chchrobotics/
[3]
http://kiwibots.org
[4]
http://www.c4learn.com/c-programming/c-bitwise-left-shift-operator/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20170426/bfdfbbfb/attachment.html>


More information about the Chchrobotics mailing list