Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,975 answers

573 users

How to round a number to a multiple of 8 in C

2 Answers

0 votes
#include <stdio.h>

// The next highest multiple of eight
unsigned int roundToMultipleOf(unsigned int number, unsigned int roundTo) {
    return (number + (roundTo - 1)) & ~(roundTo - 1);
}

int main() {
  printf("%d\n", roundToMultipleOf(9, 8));
  printf("%d\n", roundToMultipleOf(19, 8));
  printf("%d\n", roundToMultipleOf(71, 8));

  return 0;
}



/*
run:

16
24
72

*/

 



answered Jun 7, 2024 by avibootz
edited Jun 9, 2024 by avibootz
0 votes
#include <stdio.h>
#include <math.h>
 
unsigned int roundToMultipleOf(unsigned int number, unsigned int multipleOf) {
    return multipleOf * floor(number / multipleOf);
}
 
int main() {
  printf("%d\n", roundToMultipleOf(9, 8));
  printf("%d\n", roundToMultipleOf(19, 8));
  printf("%d\n", roundToMultipleOf(71, 8));
 
  return 0;
}
 
 
 
/*
run:
 
8
16
64
 
*/

 



answered Jun 8, 2024 by avibootz

Related questions

2 answers 85 views
2 answers 108 views
2 answers 119 views
2 answers 105 views
1 answer 115 views
2 answers 139 views
2 answers 102 views
...