Armenian Knowledge Base  

Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters > Algorithms
Register

Reply
 
LinkBack Thread Tools
Old 17.12.2003, 20:41   #1
Академик
 
greka's Avatar
 
Join Date: 09 2001
Location: inside myself
Posts: 5,369
Downloads: 0
Uploads: 0
Reputation: 18 | 5
Default Проооооостенькая задачка :d

Code:
	int sCount;
	unsigned int shiftedMask;

	sCount = 0L;
	shiftedMask = 0xFFFFFFFF >> (32 - sCount);
угадайте с 3-х раз чему будет равен "shiftedMask ".

P.S. советую ответ проверить на реальном приложении
Reply With Quote
Old 17.12.2003, 20:52   #2
Бакалавр
 
Join Date: 03 2002
Location: Detroit, MI, USA
Posts: 482
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default Re: Проооооостенькая задачка :d

Quote:
Originally posted by Greco El
Code:
	int sCount;
	unsigned int shiftedMask;

	sCount = 0L;
	shiftedMask = 0xFFFFFFFF >> (32 - sCount);
угадайте с 3-х раз чему будет равен "shiftedMask ".

P.S. советую ответ проверить на реальном приложении
Assuming the platform is x86, shiftedMask is undefined. Here is what standard says:

Quote:
The results are undefined if the right operand of a shift expression is negative or if the right operand is greater than or equal to the number of bits in the (promoted) left operand.
sizeof(unsigned int) = 32 ==> result of shift is undefined.
Reply With Quote
Old 17.12.2003, 20:54   #3
скромный VIP
 
analyst's Avatar
 
Join Date: 06 2003
Location: Yerevan
Age: 30
Posts: 960
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default

Обясните плз

shiftedMask = 0xFFFFFFFF >> (32 - sCount); [/B]
Reply With Quote
Old 17.12.2003, 21:05   #4
Академик
 
W_z_rd's Avatar
 
Join Date: 08 2002
Location: Yerevan, Armenia
Age: 45
Posts: 4,854
Downloads: 1
Uploads: 0
Reputation: 225 | 4
Default

Depends on platform, compiler, processor, etc.
Reply With Quote
Old 17.12.2003, 21:13   #5
Banned
 
DaNYer's Avatar
 
Join Date: 10 2002
Location: Brooklyn, New York
Age: 39
Posts: 3,760
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Default

"depends" is the best answer for everything.
Reply With Quote
Old 18.12.2003, 07:27   #6
Академик
 
greka's Avatar
 
Join Date: 09 2001
Location: inside myself
Posts: 5,369
Downloads: 0
Uploads: 0
Reputation: 18 | 5
Default

Quote:
Originally posted by W_z_rd
Depends on platform, compiler, processor, etc.

it does not depend on compiler.
I checked that up for Borland compiler, too.
Reply With Quote
Old 18.12.2003, 07:59   #7
Академик
 
greka's Avatar
 
Join Date: 09 2001
Location: inside myself
Posts: 5,369
Downloads: 0
Uploads: 0
Reputation: 18 | 5
Default

Quote:
Originally posted by analyst
Обясните плз

shiftedMask = 0xFFFFFFFF >> (32 - sCount);
[/B]
это сдвиг маски из 32-х двоичных единиц вправо на 32 бита.


ответ Tumanyan-а разъясняет все.

Мистика. Фантастика.
Reply With Quote
Old 18.12.2003, 08:53   #8
Академик
 
greka's Avatar
 
Join Date: 09 2001
Location: inside myself
Posts: 5,369
Downloads: 0
Uploads: 0
Reputation: 18 | 5
Default

лишь уточню: вышесказанное относится только к типу "int32"

если я сдвину "unsigned char" на "8" бит вправо, получу законный "0".


takie pirogi.
t.e. tortiki.
Reply With Quote
Old 18.12.2003, 19:58   #9
ЙЦУКЕН
 
Join Date: 07 2002
Location: 0x68,0x69,0x72, 0x69,0x6e,0x67, 0x20,0x6e,0x6f, 0x77
Age: 47
Posts: 3,118
Downloads: 0
Uploads: 0
Reputation: 5 | 0
Default

pered tem kak sdvigat' priwedi k unsigned .....
Reply With Quote
Old 18.12.2003, 20:28   #10
ЙЦУКЕН
 
Join Date: 07 2002
Location: 0x68,0x69,0x72, 0x69,0x6e,0x67, 0x20,0x6e,0x6f, 0x77
Age: 47
Posts: 3,118
Downloads: 0
Uploads: 0
Reputation: 5 | 0
Default

Quote:
Originally posted by nm
pered tem kak sdvigat' priwedi k unsigned .....

ja imel w widu chto-to tipa

((unsigned int)0xFFFFFFFF) >> bitcount ...


dlja signed chisla (proc/kompiler) objazan delat' rasshirenie znakowogo bita (starshego) i hren ego znaet kak imenno ono eto sdelaet ... dependz, kak gritsja .... w sluchae sdviga unsigned chisla dolzhno wse byt' tip top


effiop twoju .... poprobowal ;)
gcc version 3.3.1 [FreeBSD] 20030711 (prerelease)

--------------
int sCount;
unsigned int shiftedMask;

sCount = 0L;
shiftedMask = ((unsigned int)0xFFFFFFFF) >> 32;

wot eto daet
warning pri kompiljacii
warning: right shift count >= width of type

no predskazuemyj otwet 0x00 :))) - kak pokazywaet disassemble - eto wychislil kompiljator (prawil'no, tak i dolzhno byt':)
--------

poshli dal'she :)
int sCount;
unsigned int shiftedMask;
sCount = 0L;
shiftedMask = 0xFFFFFFFF >> (32 - sCount);

wot eto daet rezul'tat 2^31-1

tak tebja . smotriw w disassembly, notacija ATT (porjadok argumentow prjamo protivopolozhnyj intelowskomu :)))) t.e. mov SOURCE, DESTINATION :)

Code:
.....skip....
// sCount == 0
0x80484e4 <main+16>:    movl   $0x0,0xfffffffc(%ebp)
// 32 -> ecx
0x80484eb <main+23>:    mov    $0x20,%eax
0x80484f0 <main+28>:    mov    %eax,%ecx
// 32 - sCount -> ecx
0x80484f2 <main+30>:    sub    0xfffffffc(%ebp),%ecx
0x80484f5 <main+33>:    mov    $0xffffffff,%eax
0x80484fa <main+38>:    shr    %cl,%eax
.....cut....
ahha. problema processora? smotrim ;)
gcc version 3.2, sparc-sun-solaris2.7
dlja tex kto bachit na sparkowskom asme :)
Code:
// pochistit' jachejku w steke  sCount
0x10678 <main+12>:      clr  [ %fp + -20 ]
// 32 -> o1
0x1067c <main+16>:      mov  0x20, %o1
// sCount -> o0
0x10680 <main+20>:      ld  [ %fp + -20 ], %o0
// 32 - sCount -> o1
0x10684 <main+24>:      sub  %o1, %o0, %o1
// -1 !!!!! -> o0
0x10688 <main+28>:      mov  -1, %o0
// o0 >> o1 -> o0
0x1068c <main+32>:      srl  %o0, %o1, %o0
// o0 -> shiftedMask
0x10690 <main+36>:      st  %o0, [ %fp + -24 ]

wobshem sparkowskij kod nagljadno pokazal w chem imenno problema - ono schitaet,chto 0xFFFFFFFF - eto -1, dazhe pri tom chto ono objavleno unsigned ;)


wobshem standart praw - jawno najdetsja kompil', kotoryj re**** eto kak-to po swoemu :)))
Reply With Quote
Old 19.12.2003, 08:05   #11
Академик
 
greka's Avatar
 
Join Date: 09 2001
Location: inside myself
Posts: 5,369
Downloads: 0
Uploads: 0
Reputation: 18 | 5
Default

Quote:
Originally posted by nm
pered tem kak sdvigat' priwedi k unsigned .....
pered tem, kak davat' sovet, ubedis', chto on rabotaet
Reply With Quote
Old 19.12.2003, 08:43   #12
Академик
 
greka's Avatar
 
Join Date: 09 2001
Location: inside myself
Posts: 5,369
Downloads: 0
Uploads: 0
Reputation: 18 | 5
Default

bystryj gonzales.
spasibo za intro v sparkovskiy ASM.


oba kompillera dali odin i tot je rezul'tat - "-1", verno ?
tak chto eto ne ficha kompillera, skoree vsego.


predlagayu proverit' eto, sdvigaya na 33 bita.
Reply With Quote
Old 04.02.2004, 23:53   #13
Авик
 
CyberJoe's Avatar
 
Join Date: 07 2002
Location: Yerevan
Age: 30
Posts: 1,348
Downloads: 2
Uploads: 0
Reputation: 9 | 0
Default

Эх подзабыл я сдвиги надо подучить...
Reply With Quote
Sponsored Links
Reply

Thread Tools


На правах рекламы:
реклама

All times are GMT. The time now is 14:09.


Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.