Created
December 2, 2016 03:58
-
-
Save vadixidav/ec1dd3e13de0a109fa8b8f64e7cd41aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdint.h> | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| void divide_zero_error() { | |
| exit(1); | |
| } | |
| typedef struct { | |
| uint32_t q, r; | |
| } QuotRem; | |
| QuotRem divide(uint32_t nu, uint32_t de) { | |
| if (de == 0) { | |
| divide_zero_error(); | |
| } | |
| QuotRem qr; | |
| qr.q = 0; | |
| qr.r = 0; | |
| int i; | |
| for (i = 31; i >= 0; i--) { | |
| qr.r = qr.r << 1; | |
| qr.r |= (nu >> i) & 1; | |
| if (qr.r >= de) { | |
| qr.r -= de; | |
| qr.q |= 1 << i; | |
| } | |
| } | |
| return qr; | |
| } | |
| int main(int argc, char **argv) { | |
| uint32_t nu = 7; | |
| uint32_t de = 5; | |
| QuotRem qr = divide(nu, de); | |
| printf("%u / %u = {q: %u, r: %u}\n", nu, de, qr.q, qr.r); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment