AKB Forums

Go Back   AKB Forums > Technical sections > Algorithms
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Algorithms The source of algorithms for your project

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Dec 17, 2003, 20:41   #1
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
Проооооостенькая задачка :d

Code:
	int sCount;
	unsigned int shiftedMask;

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

P.S. советую ответ проверить на реальном приложении
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Dec 17, 2003, 20:52   #2
Бакалавр
 
Join Date: Mar 2002
Location: Detroit, MI, USA
Posts: 482
Rep Power: 7
Reputation: 10
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.
__________________
Hovhannes Tumanyan,
CISSP
Tumanyan is offline   Reply With Quote Quote selected
Old Dec 17, 2003, 20:54   #3
скромный VIP
 
analyst's Avatar
 
Join Date: Jun 2003
Location: Yerevan
Posts: 960
Rep Power: 6
Reputation: 10
Обясните плз

shiftedMask = 0xFFFFFFFF >> (32 - sCount); [/b]
__________________
И аткуда такие беруцца???
analyst is offline   Reply With Quote Quote selected
Old Dec 17, 2003, 21:05   #4
Академик
 
W_z_rd's Avatar
 
Join Date: Aug 2002
Location: Yerevan, Armenia
Posts: 4,781
Rep Power: 7
Reputation: 191
Send a message via ICQ to W_z_rd
Depends on platform, compiler, processor, etc.
__________________
Женщин не надо понимать, их надо любить!
W_z_rd is online now   Reply With Quote Quote selected
Old Dec 17, 2003, 21:13   #5
Banned
 
DaNYer's Avatar
 
Join Date: Oct 2002
Location: Brooklyn, New York
Posts: 3,760
Rep Power: 0
Reputation: 10
"depends" is the best answer for everything.
DaNYer is offline   Reply With Quote Quote selected
Old Dec 18, 2003, 07:27   #6
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
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.
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Dec 18, 2003, 07:59   #7
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
Quote:
Originally posted by analyst
Обясните плз

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


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

Мистика. Фантастика.
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Dec 18, 2003, 08:53   #8
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
лишь уточню: вышесказанное относится только к типу "int32"

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


takie pirogi.
t.e. tortiki.
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Dec 18, 2003, 19:58   #9
ЙЦУКЕН
 
Join Date: Jul 2002
Location: 0x68,0x69,0x72, 0x69,0x6e,0x67, 0x20,0x6e,0x6f, 0x77
Posts: 3,114
Rep Power: 7
Reputation: 10
Send a message via ICQ to nm
pered tem kak sdvigat' priwedi k unsigned .....
nm is offline   Reply With Quote Quote selected
Old Dec 18, 2003, 20:28   #10
ЙЦУКЕН
 
Join Date: Jul 2002
Location: 0x68,0x69,0x72, 0x69,0x6e,0x67, 0x20,0x6e,0x6f, 0x77
Posts: 3,114
Rep Power: 7
Reputation: 10
Send a message via ICQ to nm
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 :)))
nm is offline   Reply With Quote Quote selected
Old Dec 19, 2003, 08:05   #11
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
Quote:
Originally posted by nm
pered tem kak sdvigat' priwedi k unsigned .....
pered tem, kak davat' sovet, ubedis', chto on rabotaet
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Dec 19, 2003, 08:43   #12
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
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.
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old Feb 4, 2004, 23:53   #13
Авик
 
CyberJoe's Avatar
 
Join Date: Jul 2002
Location: Yerevan
Posts: 1,347
Rep Power: 7
Reputation: 19
Send a message via ICQ to CyberJoe
Эх подзабыл я сдвиги надо подучить...
__________________
вот собственно все, что я хотел сказать.
CyberJoe is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:46.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link