0
A
回答
0
(define (set-bit value index n)
(let ([mask (arithmetic-shift 1 index)])
(cond [(= value 0) (bitwise-and (bitwise-not mask) n)]
[(= value 1) (bitwise-ior mask n)])))
1
這是一個解決方案的開始。你能否看到剩下的情況需要做些什麼?
; bit-index->number : natural -> natural
; return the number which in binary notation has a 1 in position n
; and has zeros elsewhere
(define (bit-index->number n)
(expt 2 n))
; Example
(displayln (number->string (bit-index->number 3) 2))
; 1000
; is-bit-set? : index natural -> boolean
; is bit n set in the number x?
(define (is-bit-set? n x)
; the bitwise-and is zero unless bit n is set in the number x
(not (zero? (bitwise-and (bit-index->number n) x))))
(define (set-bit! n x b)
(cond
[(= b 1) ; we need to set bit n in x to 1
(cond
[(is-bit-set? n x) x] ; it was already set
[else (+ x (bit-index->number n))])] ; add 2^n
[(= b 0)
; <what goes here?>
]))
相關問題
- 1. 球拍/方案中的zip函數
- 2. 訪問球拍/方案中的特定列表部件
- 3. 二進制樹在球拍中的後序中列出
- 4. 覆蓋在方案(球拍)
- 5. 在方案球拍中乘以Big-int
- 6. 功能在特定區域的方案/球拍返回功能
- 7. 方案:球拍中的循環功能
- 8. 二叉樹中的方案/球拍操作(+, - ,*,/)
- 9. 二進制搜索索引位置
- 10. 方案(球拍) - 循環庫
- 11. 球拍/方案過濾
- 12. 方案/球拍:倍布爾
- 13. 球拍VS方案宏
- 14. 球拍/方案:與結構
- 15. 如何檢索球拍/ PLT方案中的按鈕標籤?
- 16. 方案/球拍 - 宏來改變程序的順序的參數
- 17. 在球拍中指定內存限制
- 18. printf%6.2f在方案或球拍?
- 19. 在VS解決方案中更改引用項目的位置
- 20. 二進制搜索結構數組中的特定值
- 21. 在列表中的二進制搜索和引用列表的特定列
- 22. 在源代碼中無更改的二進制中的更改
- 23. 定製二進制搜索中的ArrayIndesOutOfBoundsException?
- 24. 包含球拍/方案內的文件
- 25. 在二進制文件中的特定位置跳轉
- 26. 在線性時間內找到二進制圖像中的特定索引?
- 27. 在二進制中更改OBJC_IVAR_ $
- 28. 解釋球拍中的圖案匹配?
- 29. 在球拍/方案中的語言嵌套循環
- 30. 沒有索引的MongoDB數組中的二進制搜索
你能解釋一下你在那裏做了什麼嗎? (此外,爲了以防萬一,要改變的位的索引必須從右到左,從1到n ...)計算 – BVtp
只要答案可能是好的,那麼給予更多一點是有意義的細節。 –