Check if an integer is a multiple of 8

Hi, I'm new to C ++, so I'm not sure if this is a really stupid question. I mainly use the C ++ custom action project to interact with my MSI installer. I get the property that my user entered, this is an integer. I need to make sure this is a multiple of 8, and I'm not sure how to do this. Obviously, if it can be divided by 8, it is somewhat, but I'm not sure how to do it if there is a remainder. Any help would be appreciated or even point me in the right direction. Thanks

+4
source share
4 answers

Use the "modulo" operator, which gives the remainder of the division:

if (n % 8 == 0) { // n is a multiple of 8 } 
+16
source

Use "modulo" or "integer break statement" % :

 int a = ....; if (a % 8 == 0 ) { // a is amultiple of 8 } 
+5
source

use the% operator

 if ( num % 8 == 0 ) { // num is multple of 8 } 
+3
source

I saw someone using a bit operation

 bool f( int x){ return !(x & 7); } 

It has been said that this approach has some problems, but I'm not quite sure.

0
source

Source: https://habr.com/ru/post/1435251/


All Articles