When trying to find out if a number is even or odd: AND 1 or OR 0

I recently learned of the trick for finding out if a binary number is odd or even.

If the last bit in the number is 0, then the number is even. If it ends in a 1, then the number is odd.

In the tutorial in which I read this, is stated that this could be applied to programming if you “AND” the number with the binary number 1.

In another tutorial that I read, they said that is could applied if you “OR” the number with binary 0.

In regards to efficiency, which method would be more efficient for finding out if a number is odd or even?

5

Ultimately, this depends on the processor or the interpreter you are using. I have not encountered a situation in which these operations differed respectably in execution time.

A GMP developer maintains a paper on x86/amd64 instruction latencies and throughput. This paper shows that both AND and OR instructions have the same timings when both arguments are registers. When one of the operands is a memory address, of course timing might change when that memory is not in cache. But this is not a function of the instruction.

Unfortunately, I can’t speak to other processors or whether people have done asinine things in interpreters that would cause a performance disparity.

There is a Bit Test instruction in the x86 instruction set (since 80386):

BT copies a bit from a given register to the carry flag.

As I understand it, you can then execute a JC (jump if carry) instruction to make a decision based on the carry flag. That’s a total of two elemental instructions and uses only one register.

However, I doubt that’s more efficient than using AND with 1 and jump if the zero flag is set. Both alternatives are two elemental CPU instructions. It’s possible the constant to specify the bit in the BT instruction uses fewer bits than the constant operand of the AND instruction, but I’m not going to bother looking it up. 🙂

That probably means the OR method is less efficient because you’d have to do the OR, then a compare to the original number and jump if equal or not equal, which at the very least is tying up 2 registers instead of 1.

A slightly better alternative than AND might be the TEST instruction because it performs an AND but throws away the result of the operation and only keeps the flags set. Since you don’t actually want to keep the result, then TEST eax, 1 followed by jump if zero (JZ) might be your best bet (since it’s possible you might want to do something else with the original value and this leaves it loaded in the register).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

When trying to find out if a number is even or odd: AND 1 or OR 0

I recently learned of the trick for finding out if a binary number is odd or even.

If the last bit in the number is 0, then the number is even. If it ends in a 1, then the number is odd.

In the tutorial in which I read this, is stated that this could be applied to programming if you “AND” the number with the binary number 1.

In another tutorial that I read, they said that is could applied if you “OR” the number with binary 0.

In regards to efficiency, which method would be more efficient for finding out if a number is odd or even?

5

Ultimately, this depends on the processor or the interpreter you are using. I have not encountered a situation in which these operations differed respectably in execution time.

A GMP developer maintains a paper on x86/amd64 instruction latencies and throughput. This paper shows that both AND and OR instructions have the same timings when both arguments are registers. When one of the operands is a memory address, of course timing might change when that memory is not in cache. But this is not a function of the instruction.

Unfortunately, I can’t speak to other processors or whether people have done asinine things in interpreters that would cause a performance disparity.

There is a Bit Test instruction in the x86 instruction set (since 80386):

BT copies a bit from a given register to the carry flag.

As I understand it, you can then execute a JC (jump if carry) instruction to make a decision based on the carry flag. That’s a total of two elemental instructions and uses only one register.

However, I doubt that’s more efficient than using AND with 1 and jump if the zero flag is set. Both alternatives are two elemental CPU instructions. It’s possible the constant to specify the bit in the BT instruction uses fewer bits than the constant operand of the AND instruction, but I’m not going to bother looking it up. 🙂

That probably means the OR method is less efficient because you’d have to do the OR, then a compare to the original number and jump if equal or not equal, which at the very least is tying up 2 registers instead of 1.

A slightly better alternative than AND might be the TEST instruction because it performs an AND but throws away the result of the operation and only keeps the flags set. Since you don’t actually want to keep the result, then TEST eax, 1 followed by jump if zero (JZ) might be your best bet (since it’s possible you might want to do something else with the original value and this leaves it loaded in the register).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật