Monthly Archives: August 2020

Node.js event .once 使用時機

Node.js EventEmitter裡註冊接收事件一般使用 .on(‘event name’, handler) 的方式,但有一個api是 .once,也就是註冊後觸發listener會自動remove,可參考 https://nodejs.org/docs/latest-v12.x/api/events.html#events_emitter_once_eventname_listener .once 在一般情境較少用到,原因是處理接收事件常常是跟著發送事件的物件生命週期(例如stream的’data’事件,.once只做一次性的handle,能使用到的情境比較像是需要接收到狀態變更,系統落入A狀態要等待切換到B狀態,因此在A狀態時註冊一個接收切換到B狀態的listener,一旦切換到B狀態就remove listener,等待下一次落入A狀態時再註冊 在Node.js doc介紹stream的地方有個不錯的範例 https://nodejs.org/docs/latest-v12.x/api/stream.html#stream_stream 節錄於下 可以注意 當writer.write 失敗時,就休息等待’drain’ event,再繼續write,也就是如上描述的 – 等待新的狀態變更。

Posted in nodejs | 1 Comment

two’s complement overflow detection

two’s complement overflow有兩種情況會發生 carry in without carry out carry out without carry in 用8 bit signed int為例 -100 + (-30) 1001 1100 + 1110 0010 ———— 1 0000 0000 <- carry bit C S = 1 0111 1110 C的位置是carry out(就是我們一般說的carry), … Continue reading

Posted in C Language | Leave a comment

one’s & two’s complement number system

在計算機的底層是用0、1來表示資料,因此人類的符號系統要透過電腦表示就需要做編碼轉換,ASCII、Unicode等都是做此用途,而在(整數)數字系統的編碼,比較常見的有 ASCII (將數字用ASCII表示) -200 => 2D 32 30 30 BCD 189 => 01 08 09 (packed BCD) one’s complement (assume 8-bit size) 0 => 0000 0000b -0 => 1111 1111b (unsigned = 255) -1=> 1111 1110b (unsigned = 254) 127 … Continue reading

Posted in C Language | Leave a comment